📄 add50.java
字号:
//Powered by Li Gang, 421341
//3.10 Make a program to add natural number 1 to 50 with 3 method
import java.io.*;
//To count natural number 1+2+...+50
class Count {
public static void addf(){ //Use the "for" struct
int sum=0;
for(int i=1; i<=50; i++) { sum +=i; }
System.out.println("Great! 1+2+3+...+50 = "+sum);
}
public static void addw(){ //Use the "while" struct
int i=1; int sum=0;
while(i<=50) { sum +=i; i++; }
System.out.println("Great! 1+2+3+...+50 = "+sum);
}
public static void addd(){ //Use the "do-while" struct
int i=1; int sum=0;
do { sum +=i; i++; } while(i<=50);
System.out.println("Great! 1+2+3+...+50 = "+sum);
}
}
//To get value of the key from the keyboard
class Get {
public static int readInt() {return Integer.parseInt(readStr());}
public static String readStr(){
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str=" ";
try {str=br.readLine();}
catch (IOException ex) {System.out.println(ex);}
return str;
}
}
//the primary class
class add50 {
public static void main(String []args){
System.out.println("\n===========Use the follow 3 methods to add natural number 1 to 50==========="+"\n");
System.out.println(" 1. Use the struct for ");
System.out.println(" 2. Use the struct while ");
System.out.println(" 3. Use the struct do-while "+"\n");
System.out.println("===================Please input ,then press the key Enter===================");
System.out.print("Option: ");
int a; a=Get.readInt(); //Move the value of the key from the keyboard to var a
switch(a){
case 1: Count.addf(); break;
case 2: Count.addw(); break;
case 3: Count.addd(); break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -