在JOptionPane中确认,我可以循环回扫描仪吗?

时间:2014-07-09 01:09:24

标签: java loops java.util.scanner joptionpane

我是新手,如果我有点慢,请原谅我。

如果我在JOptionPane中有一个确认对话框,如果用户状态为YES,可以循环回扫描程序吗? EX:在JOptionPane中,如果用户回答是,那么我希望它循环回来询问Power和Exp并调用结果的方法 - 继续重复直到用户确认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;
}
  }
}

3 个答案:

答案 0 :(得分:0)

您可以在while循环中包含与用户输入(而不是pow方法)直接相关的所有代码,终止条件类似于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是有效响应,而其他任何因素都会导致对话框再次出现...

您还需要为第一个代码块做一些工作。您检查用户输入是否小于一个数字,然后直接调用扫描仪。这意味着将检查第一个数字,但第二个数字将被接受,无论它是什么......

答案 1 :(得分:0)

do-while循环中运行代码,直到用户点击“否”&#39;

 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)

答案 2 :(得分:0)

谢谢大家。我决定调用一个方法而不是一个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");
}
}