請撰寫程式,宣告一個變數i,如果i的值小於31,就顯示2的i次方的值。
import java.io.*;
public class ex4_1{
public static void main(String[] argv)
throws IOException{
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
System.out.println("→");
String i = b.readLine();
int y = Integer.parseInt(i);
if(y <=30){
long j = 1;
for(int x = 0;xj = j * 2;
}
System.out.println(j);
}
else {
System.out.println("超出數值,請重新輸入");
}
}
}
import java.io.*;
public class ex4_1{
public static void main(String[] argv)
throws IOException{
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
System.out.println("→");
String i = b.readLine();
int y = Integer.parseInt(i);
if(y <=30){
long j = 1;
for(int x = 0;xj = j * 2;
}
System.out.println(j);
}
else {
System.out.println("超出數值,請重新輸入");
}
}
}
文章標籤
全站熱搜

import java.util.*; public class test777{ public static void main(String[] argv){ System.out.println("Enter number:"); Scanner sc = new Scanner(System.in); while(!sc.hasNextInt()){System.out.println("Enter again:"); sc.next();} int n = sc.nextInt(); int i,sam=1; if(n<31){ for(i=1;i<=n;i++){ sam = sam*(i/i)*2; } System.out.println("2的"+n+"次方="+sam); } else{System.out.println("i="+n); } } }