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