import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("-------Welcome to the Radius calculator-------");
String input = new String (" ");
while(input.equals("END")==false) {
for (int i = 1; i < 5; i )
for (int e = 5; e > 0; e--)
{
{
System.out.println("-----------------------------");
System.out.println("Hey you can use this calcultor " e " more time(s) till yoh have to purchase our full version only 3.99");
}
System.out.println("Please enter your Radius if not just type in END at any stage during the program");
Double num =sc.nextDouble();
System.out.println("Enter 1 if you would like to get the area");
System.out.println("Enter 2 if you would like to get the circumfrence");
int num1 = sc.nextInt();
if (num1 == 1) {
System.out.println("The area of the circle with the radius " num " is :" (Math.PI) * (num * num));
} else if (num1 == 2) {
System.out.println("The circumfrence of the circle with the radius " num " is :" (2) * (Math.PI) * (num));
} else {
System.out.println("No answer for you boss");
System.out.println("Try again!");
}
System.out.println("You have used this calculator effiently " i " time(s)");
System.out.println("-----------------------------");
}
}
}
}
這是我的代碼,但當我輸入 END 時出現錯誤。(https://i.stack.imgur.com/ZZxEW.png)](https://i.stack.imgur.com/ZZxEW.png )
如果有任何其他技巧可以使我的代碼高效,我也會很高興。
uj5u.com熱心網友回復:
您忘記在回圈期間詢問結束輸入。
請求不同型別輸入的最佳方法是使用 BufferedReader 類
BufferedReader reader =new BufferedReader(newInputStreamReader(System.in));
String input = null;
try {
input = reader.readLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
if (input.equals("END")) {
break;
}
之后你可以使用:
Integer.parseInt(input); 從 String 轉換為 Integer
并且不要忘記清理和簡化您的代碼。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/526648.html
上一篇:numeric()和as.numeric()有什么區別?
下一篇:在Lua中使用for回圈創建變數
