[英]Confirmation in JOptionPane, can I loop back to scanner?
我是新手,如果我有點慢,請原諒我。
如果我有一個JOptionPane的確認對話框,可以循環回掃描器如果用戶狀態是? EX:在JOptionPane中,如果用戶回答是,那么我就希望回過頭來,要求電力和實驗,並調用方法的結果 - 不斷重復,直到用戶確認NO。 那有意義嗎?
我的完整代碼如下。
import java.util.Scanner;
import javax.swing.JOptionPane;
public class XtoNthPowerRecursively {
//Main method
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter base to be raised to power: ");
double Base = input.nextDouble();
System.out.print("Enter exponent to raise base to: ");
int Exp = input.nextInt();
if (Exp < 0) {
System.out.print("Exponent cannot be negative. Re-enter: ");
Exp = input.nextInt();
}
//Print Base and Exp with result
System.out.println("Base " + Base + " raised to the power " + Exp + " is " + power(Base, Exp));
//JOptionPane confirmation dialog
int n = JOptionPane.showConfirmDialog(null,"Would you like to enter another base and power?", "Confirmation",JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION)
{
JOptionPane.showMessageDialog(null,"Let's start");
}
else
{
JOptionPane.showMessageDialog(null,"Goodbye");
}
}
//Calling pow method
public static double power(double Base, int Exp) {
if (Exp < 0 ) {
return power(Base, ++Exp)/Base;
} else if (Exp == 0) {
return 1.0;
} else {
return power(Base, --Exp) * Base;
}
}
}
您可以將所有與用戶輸入直接相關的代碼(而不是pow方法)封裝在while
循環中,終止條件類似於boolean carryOn = false
。例如,
while (carryOn)
{
Do your code here.
Show the dialog box.
If (userInput.equalsIgnoreCase("no"))
carryOn = false;
System.out.println("Goodbye"); // The loop terminates.
else if (userInput.equalsIgnoreCase("yes"))
carryOn = true;
// User is returned to the start of the loop to enter another number.
}
您還可以將if/else
語句也包含在循環中,以確保只有yes和no是有效的響應,並且其他任何原因都會導致對話框再次出現...
您還需要對第一段代碼做一些工作。 您檢查用戶輸入的數字是否小於數字,然后立即致電掃描儀。 這意味着將檢查第一個數字,但是第二個數字將被接受。
在do-while
循環中運行代碼,直到用戶單擊“否”
do{
Scanner input = new Scanner(System.in);
System.out.print("Enter base to be raised to power: ");
double Base = input.nextDouble();
System.out.print("Enter exponent to raise base to: ");
int Exp = input.nextInt();
if (Exp < 0) {
System.out.print("Exponent cannot be negative. Re-enter: ");
Exp = input.nextInt();
}
//Print Base and Exp with result
System.out.println("Base " + Base + " raised to the power " + Exp + " is " + power(Base, Exp));
//JOptionPane confirmation dialog
int n = JOptionPane.showConfirmDialog(null,"Would you like to enter another base and power?", "Confirmation",JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION) {
JOptionPane.showMessageDialog(null,"Let's start");
} else {
JOptionPane.showMessageDialog(null,"Goodbye");
}
}while (n != JOptionPane.NO_OPTION)
感謝大家。 我決定調用一個方法而不是一個do-while循環。 最終代碼如下。
public class XtoNthPowerRecursively
//Main method
public static void main(String[] args) {
run ();
}
public static void run() {
Scanner input = new Scanner(System.in);
System.out.print("Enter base to be raised to power: ");
double Base = input.nextDouble();
System.out.print("Enter exponent to raise base to: ");
int Exp = input.nextInt();
while (Exp < 0) {
System.out.print("Exponent cannot be negative. Re-enter: ");
Exp = input.nextInt();
}
//Print Base and Exp with result
System.out.println("Base " + Base + " raised to the power " + Exp + " is " + power(Base, Exp));
//JOptionPane confirmation dialog
int n = JOptionPane.showConfirmDialog(null,"Would you like to enter another base and power?", "Confirmation",JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION)
{
JOptionPane.showMessageDialog(null,"Let's start");
run ();
}
else
{
JOptionPane.showMessageDialog(null,"Goodbye");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.