1.求任意輸入的10個數的和。?

import java.util.Scanner;
public class Test1 {
public static void main(String[] args){
Scanner sca=new Scanner(System.in);
double sum=0;//定義一個雙精度浮點數作為結果,并設初值為0
int input=1;//定義一個整形input設初始值為1,防止循環直接結束
System.out.println("請輸入數字:");
while(input!=0){//while實現元素的循環輸入
input=sca.nextInt();
sum+=input;//求和
}
System.out.println("sum="+sum);
}
}2.實驗題目:獲取三個整數中的大值(用三元運算符)。
import java.util.Scanner;
public class Test2 {
public static void main(String[] args){
Scanner sca=new Scanner(System.in);
int a,b,c;
System.out.println("請輸入要比較的三個整數:");
System.out.print("請輸入第一個數:");
a=sca.nextInt();
System.out.print("請輸入第二個數:");
b=sca.nextInt();
System.out.print("請輸入第三個數:");
c=sca.nextInt();
int max= a>b?(a>c?a:c):(b>c?b:c);
System.out.println("大值為:"+max);
}
}3.鍵盤錄入月份的值,輸出對應的季節。
import java.util.*;
public class Test3{
public static void main(String[] args){
System.out.print("請輸入月份:");
Scanner sca=new Scanner(System.in);
int mouth=sca.nextInt();
// if (mouth == 2 || mouth == 3 || mouth == 4) {
// System.out.println(mouth + "月是春季");
// } else if (mouth == 5 || mouth == 6 || mouth == 7) {
// System.out.println(mouth + "月是夏季");
// } else if (mouth == 8 || mouth == 9 || mouth == 10) {
// System.out.println(mouth + "月是秋季");
// } else if (mouth == 11 || mouth == 12 || mouth == 1){
// System.out.println(mouth + "月是冬季");
// }else{
// System.out.println(mouth + "月不存在");
// }
switch(mouth){
case 2:
case 3:
case 4:
System.out.println(mouth + "月是春季");
break;
case 5:
case 6:
case 7:
System.out.println(mouth + "月是夏季");
break;
case 8:
case 9:
case 10:
System.out.println(mouth + "月是秋季");
break;
case 11:
case 12:
case 1:
System.out.println(mouth + "月是冬季");
break;
default:
System.out.println(mouth + "月不存在");
}
}
}4.輸入年份和月份,輸出該年月的天數。
import java.util.Scanner;
public class Test4 {
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
System.out.print("請輸入年份和月份:");
int year,month;
int days=0;
year=scan.nextInt();
month=scan.nextInt();
boolean isLeapYear;
isLeapYear = (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
switch(month){
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
days=31;
break;
case 2:
if(isLeapYear){
days=29;
}else{
days=28;
}
break;
case 4:
case 6:
case 9:
case 11:
days=30;
break;
default:
System.out.println("error!!!");
}
System.out.println(year+"年"+month+"月有"+days+"天");
}
}5.出租車計費問題。
? 開封市的出租車計費方式為:起步2公里內5元,2公里以上每公里收費1.3元,9公里以上每公里收費2元,燃油附加費1元。
? 編寫程序,輸入公里數,計算出所需的出租車費用。
public class Test5 {
public static void main(String[] args){
Scanner sca=new Scanner(System.in);
System.out.print("請輸入公里數:");
int km=sca.nextInt();
double money;
if(km<=2){
money=5;
}else if(km>2&&km<9){
money=5+(km-2)*1.3;
}else{
money=5+7*1.3+(km-9)*3;
}
System.out.println("你所需的出租車費用是"+money);
}
}6.分別用do-while和for循環計算1+1/2!-1/3!+1/4!-1/5!…的前20項之和。
public class Test6 {
public static void main(String[] args) {
double a = 1, b = 1;
double sum = 0;
System.out.print("do-while循環:");
do {
sum += b;
a++;
b *= 1.0 / a;
} while (a<= 20);
System.out.println(sum);
// int i,j;
// for(i=1;i<=20;i++){
// float t=1;
// for(j=1;j<=i;j++){
// t=t*j;
// }
// sum+=(1/t);
// }
System.out.print("for循環:");
for (a = 1; a<= 20; a++) {
b *= (1.0 / a);
sum += b;
}
System.out.println(sum);
}
} 7.求1000以內的完全數(一個數等于它的因子之和稱為完全數)。
public class Test7 {
public static void main(String[] args) {
int i;
int k=0;
for(i=1;i<=1000;i++){
int sum=0;
for(int j=1;j8.微信中的一個問題:一筐雞蛋,1個1個拿,正好拿完。
2個2個拿,還剩1個。
3個3個拿,正好拿完。
4個4個拿,還剩1個。
5個5個拿,還差1個。
6個6個拿,還剩3個。
7個7個拿,正好拿完。
8個8個拿,還剩1個。
9個9個拿,正好拿完。
問筐里最少有多少雞蛋?
public class Test8 {
public static void main(String[] args) {
int i=0;
while (true){
i++;
if(i%2==1&&i%4==1&&i%5==4&&i%8==1){
if(i%3==0&&i%7==0&&i%9==0){
if(i%6==3){
break;
}
}
}
}
System.out.println("筐里最少有"+i+"個雞蛋");
}9.求六邊形面積,六邊形面積可以通過下面公式計算(s是邊長):
注:使用Math類中的方法計算tan值。
import java.util.Scanner;
public class Test9 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("請輸入邊長:");
double s = input.nextDouble();//用戶輸入數據
//判斷數據的合法性
if(s<0){
System.out.println("輸入的數字不合法");
}
//進行面積運算
double area = (6 * s * s) / (4 * Math.tan(Math.PI / 6));
//輸出面積
System.out.printf("六邊形的面積是:"+area);
}
}10.實現會員注冊,要求用戶名長度不小于3,密碼長度不小于6,若不滿足需有提示信息,提示輸入有誤;注冊時兩次輸入密碼必須相同(字符串)。
import java.util.Scanner;
public class Test10 {
public static void main(String[] args){
Scanner sca=new Scanner(System.in);
String name;
String password;
String newpassword;
System.out.println("用戶注冊");
System.out.print("用戶名:");
name=sca.next();
System.out.print("設置密碼:");
password=sca.next();
System.out.print("確認密碼:");;
newpassword=sca.next();
while(name.length()< 3||(password.length())<6||(password.equals(newpassword)==false)){
if(name.length()<3) {
System.out.println("用戶名長度不得小于3");
}
if(password.length()<6){
System.out.println("密碼長度不得小于6");
}
if(password.equals(newpassword)==false){
System.out.println("兩次密碼不一致");
}
System.out.print("請重新輸入用戶名:");
name = sca.next();
System.out.print("請重新輸入密碼:");
password = sca.next();
System.out.print("請再次輸入密碼:");
newpassword = sca.next();
}
System.out.println("用戶名:"+name);
System.out.println("密碼:"+password);
System.out.println("注冊成功!");
}
}11.找出兩個分教最高的學生)編寫程序,提示輸入學生的個數、每個學生的名字及其分數,最后顯示獲得最高分的學生和第二高分的學生。
import java.util.Scanner;
public class Test11 {
public static void main(String[] args){
Scanner sca=new Scanner(System.in);
System.out.print("請輸入學生的數量:");
int n=sca.nextInt();
if(n<2)
{
System.out.println("輸入學生的個數過少");
System.exit(0);//結束程序
}
String[] stuname=new String[n];
int[] score=new int[n];
for(int i=0;i12.定義一維數組并初始化,通過鍵盤任意輸入一個數,查找該數是否存在(結果返回下標值)。
import java.util.Scanner;
public class Test12 {
public static void main(String[] args){
Scanner sca=new Scanner(System.in);
int[] arr=new int[]{10,20,30,40,50};
System.out.print("請輸入你要查找的數字:");
int n=sca.nextInt();
int i=0,p=-1;
// for(i=0;i=0){
// System.out.println("所求下標為"+p);
// }else if(p==-1){
// System.out.println("該數字未在數組中。");
// }
while(i=0){
System.out.println("所求下標為"+p);
}else if(p==-1){
System.out.println("該數字未在數組中。");
}
}
} 13.編寫一個程序,將二維數組a轉置后存入數組b(所謂轉置就是行列互換)例如:
1 2 3
4 5 6
7 8 9 ??
的轉置就是:
public class Test13 {
public static void main(String[] args) {
int[][] arr1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int[][] arr2=new int[3][3];
for(int i = 0; i你是否還在尋找穩定的海外服務器提供商?創新互聯www.cdcxhl.cn海外機房具備T級流量清洗系統配攻擊溯源,準確流量調度確保服務器高可用性,企業級服務器適合批量采購,新人活動首月15元起,快前往官網查看詳情吧
本文標題:Java程序設計---實驗2-創新互聯
當前網址:http://www.js-pz168.com/article46/gsshg.html
成都網站建設公司_創新互聯,為您提供網頁設計公司、App設計、小程序開發、微信公眾號、外貿建站、網站導航
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯