Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中创建循环?_Java_Loops - Fatal编程技术网

在java中创建循环?

在java中创建循环?,java,loops,Java,Loops,在请求用户输入时,我一直在努力找出如何使代码循环 基本上,如果用户根本不输入文本,我希望程序重新提问。 这就是我到目前为止所做的 import javax.swing.JOptionPane; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Assessment { public static void main(St

在请求用户输入时,我一直在努力找出如何使代码循环

基本上,如果用户根本不输入文本,我希望程序重新提问。 这就是我到目前为止所做的

import javax.swing.JOptionPane;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

  public class Assessment {

    public static void main(String[] args) throws IOException {

          BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));

          String me = JOptionPane.showInputDialog("Please enter your name");
          System.out.println("Your name is: " + me);

          String user1 = JOptionPane.showInputDialog("Please choose a number");
          System.out.println("Your number is: " + user1);

          String user2 = JOptionPane.showInputDialog("Please enter your choice of    security, choice1(low) or choice2(high)");
          String response = (String)System.in.toString();

          if(user2.equals("choice1"))
            JOptionPane.showMessageDialog(null,"your username is: "+me+user1,"Your username",JOptionPane.WARNING_MESSAGE);


    }
}
要在Java中比较
字符串
,必须使用equals(),因为您不希望它等于空文本,所以应该在Java中使用否定


希望有帮助。

您可以使用while循环来引用他们输入的内容: 比如while input=“{question}甚至是do while循环。 见这个问题:

这也可能有助于:
使用java.util.Scanner

Scanner input = new Scanner(System.in);
提示输入名称后,请使用以下命令:

String name = input_next();
if (name != null && !("").equals(name)) {  //then move next - else go back to the prompt

这就是你需要做的-

   String me = "";

          do
          {   
            me = JOptionPane.showInputDialog("Please enter your name");
          }while(me.equals(""));
在其他窗口也要这样做

或者,只需复制粘贴此代码:(


你可以找到一个很好的教程,特别是“while”和“do-while”语句

简言之:

while (condition) { 
   // statements
}


只要条件的计算结果为true,这些语句就会继续执行。while和do-while之间的区别是计算条件的时间,有关详细信息,请参阅教程。

搜索do-while结构。这非常有效:)感谢您的帮助@user3063011-太好了。你需要学习更多的东西。顺便说一句,请选择我的答案。谢谢你的帮助:)
import javax.swing.JOptionPane;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Assessment {

    public static void main(String[] args) throws IOException {

        BufferedReader userInput = new BufferedReader(new InputStreamReader(
                System.in));

        String me = "";
        String user1 = "";
        String user2 = "";

        do {
            me = JOptionPane.showInputDialog("Please enter your name");
        } while (me.equals(""));

        System.out.println("Your name is: " + me);

        do {
            user1 = JOptionPane.showInputDialog("Please choose a number");
        } while (user1.equals(""));
        System.out.println("Your number is: " + user1);

        do {
            user2 = JOptionPane
                    .showInputDialog("Please enter your choice of    security, choice1(low) or choice2(high)");

        } while (user2.equals(""));
        String response = (String) System.in.toString();

        if (user2.equals("choice1"))
            JOptionPane.showMessageDialog(null, "your username is: " + me
                    + user1, "Your username", JOptionPane.WARNING_MESSAGE);

    }
}
while (condition) { 
   // statements
}
do {
   // statements
} while (condition);