简体   繁体   English

你好! 最近开始学习Java并且无法重复程序指令

[英]Hello! Started learning Java recently and having trouble repeating program instructions

I am new to Java and programming outside of VB in general and I am looking for some basic help. 我是Java新手以及VB之外的编程,我正在寻找一些基本的帮助。 I wrote the following code below and I want the program to repeat until the user types stop. 我在下面写了下面的代码,我希望程序重复,直到用户输入停止。

import java.util.Scanner;

public class lame
{
     public static void main (String args[])
     {
          System.out.println("Welcome to robo lame tester 1.1, is your name Connor? Yes or no?");
          Scanner input = new Scanner(System.in);
          String sweg = input.nextLine();
          if (sweg.equals("Yes"))
               System.out.println("You are lame");
          else 
               System.out.println("You passed, you aren't lame");
     }
}

I typed this in real quick... but it should do what you ask. 我快速输入了这个...但它应该按照你的要求做。 So I added a while true that would make it loop forever. 所以我添加了一段时间才能使它永远循环。 The while(condition) statement will loop until condition is given a false statement. while(condition)语句将循环,直到条件被赋予false语句。 The other way to leave would be to break the loop with the break statement or the return statement (as I did in there). 另一种离开的方法是使用break语句或return语句打破循环(正如我在那里所做的那样)。 break will make you leave the loop and return will make you leave the method. break会让你离开循环并返回会让你离开这个方法。

import java.util.Scanner;

public class Lame {
    public static void main (String args[])
    {
        while(true) {
         System.out.println("Welcome to robo lame tester 1.1, is your name Connor? Yes or no?");
         Scanner input = new Scanner(System.in);
         String sweg = input.nextLine();
         if(sweg.equals("stop"))
             return;
         if (sweg.equals("Yes"))
              System.out.println("You are lame");
         else 
              System.out.println("You passed, you aren't lame");
        }
    }
}

Insert it in a loop, as this: 将其插入循环中,如下所示:

import java.util.Scanner;

public class Lame {

    public static void main( String args[] ) {
        String sweg = "";
        do {
            System.out
                    .println( "Welcome to robo lame tester 1.1, is your name Connor? Yes or no?" );
            Scanner input = new Scanner( System.in );
            sweg = input.nextLine();
            if ( sweg.equalsIgnoreCase( "Yes" ) ) {
                System.out.println( "You are lame" );
            } else if ( sweg.equalsIgnoreCase( "no" ) ) {
                System.out.println( "You passed, you aren't lame" );
            }
        } while ( !sweg.equalsIgnoreCase( "stop" ) );
        System.out.println( "Bye!" );
    }
}

Adendum Adendum

Class names begin with a capital by convention in Java. 类名以Java中的约定开头。 String's equalsIgnoreCase is recommended for user input, so they don't need to maintain the case. 建议将String的equalsIgnoreCase用于用户输入,因此不需要维护大小写。

Here is how 这是怎么回事

Scanner input = new Scanner(System.in);
String sweg = null;
while(!((sweg =input.nextLine()).equals("stop"))){
    System.out.println("you are lame");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM