不等待java输入

不等待java输入,java,java.util.scanner,Java,Java.util.scanner,我有一个程序,应该接受整数输入,根据输入输出一些字符,然后提示再次运行该程序。例如 Please enter an integer --> 3 x xx xxx xx x Do you want to run again? 这是我的程序的代码: import java.util.*; public class CMIS242Assignment1Stars { public static void main(String[] args) {

我有一个程序,应该接受整数输入,根据输入输出一些字符,然后提示再次运行该程序。例如

Please enter an integer --> 3

x

xx

xxx

xx

x

Do you want to run again?
这是我的程序的代码:

import java.util.*;
public class CMIS242Assignment1Stars 
{
    public static void main(String[] args) 
    {
        String again;

       do //start of "run again" loop
        {
            System.out.print("Input a positive integer and press [ENTER]--> ");
            Scanner input = new Scanner(System.in);

            if (input.hasNextInt()) // check if input is parsable to an int
            {        
                int num = Integer.parseInt(input.next());

                if (num <= 0) //check if num is positive
                {
                    System.out.println(num + " is not a positive integer. Using +" + (num*-1) + " instead.");
                    num = num *= -1;
                }    
                String stars = new String(new char[num]).replace("\0", "*"); // create a string of '*' of length 'num'
                int forNum = num * 2;
                int flip = 0;

                for (int x = 0; x <= forNum ; x++)
                {
                    System.out.println(stars.substring(0,stars.length() - num)); //create substrings of variable length from 'stars'

                    if(num <= 0)
                    {
                        flip = 1;
                    }

                    if(flip == 0)
                    {
                        num--;
                    }
                    else
                    {
                        num++;
                    }
                }
            }           
            else 
            {
                System.out.println("ERROR: Please input a positive integer!");//error message if a non-integer is entered
            }

            System.out.print("Would you like to run again? [Yes / No] ");
            again = input.next();     
        }
        while(again.equalsIgnoreCase("yes") || again.equalsIgnoreCase("y")); // end of "run again" loop

        System.out.print("Good Bye!"); //exit message        
    }
}
import java.util.*;
公共类CMIS242分配1星
{
公共静态void main(字符串[]args)
{
再串一次;
执行//开始“再次运行”循环
{
System.out.print(“输入一个正整数,然后按[ENTER]-->”);
扫描仪输入=新扫描仪(System.in);
if(input.hasNextInt())//检查输入是否可解析为int
{        
int num=Integer.parseInt(input.next());

如果(num您需要稍微修改一下逻辑

对您来说,最简单的解决方案是修复else语句

else 
{
  //Move scanner position.
  String badInput = input.next();
  System.out.println("ERROR: Please input a positive integer!");//error message if a non-integer is entered
}

检查是否
input.hasNextInt()
,但它不检查,控制台中的某些内容不是整数。
hasNextInt()
方法在使用
hasNextInt()
时实际上不会移动扫描仪位置。要解决此问题,我们使用
input.next()
在else语句中。

原始代码中的
badInput
再次被分配到
,这会导致意外的结果。我在原始代码中没有看到任何名为badInput的内容。你是什么意思?我不明白。很抱歉误解。从输入中收集到的坏
字符串
,即您分配给
badInput
,正在(而不是)在原始代码中再次分配给变量
。原始代码缺少您建议的收集
不良输入的步骤,这是正确的。是的,我是,您也是。:P我已在答案中添加了解释。谢谢!谢谢!程序现在运行得很好!
else 
{
  //Move scanner position.
  String badInput = input.next();
  System.out.println("ERROR: Please input a positive integer!");//error message if a non-integer is entered
}