2017-02-22 42 views
0

使用Java编译接受任何输入的代码,从字符串到双精度,浮点数,整数等,但只能基于整数进行处理。如果它收到一个double,float或者一个字符串,它只会产生一条提示用户再次尝试的消息。我不确定如何滚动,所以这是我现在的代码。仅限严格整数如果/然后循环

import java.util.Scanner; 

public class Sand 
{ 
    public static void main(String[] args) 
    { 
    int firstInt, secondInt, thirdInt; 

    Scanner keyboard = new Scanner(System.in); 
    System.out.println("Hi. I'm going to be asking you for three integers in just a moment."); 
    System.out.println("An integer is defined as a WHOLE number that's not a fraction or decimal."); 
    System.out.println("I swear to you, I will go off if you put in a non-whole number..."); 
    System.out.println("Okay go ahead and type in the first of the three integers:"); 

    if (keyboard.hasNextInt()) 
    { 
     firstInt = keyboard.nextInt(); 
     System.out.println("Red."); 
     System.out.println("So the first integer is " + firstInt); 
     System.out.println("Okay go ahead and type in the second of the three integers: "); 
     if (keyboard.hasNextInt()) 
     { 
      secondInt = keyboard.nextInt(); 
      System.out.println("Green."); 
      System.out.println("So the first and second integers are " + firstInt + " and " + secondInt); 
     } 

     else 
     { 
      System.out.println("Orange."); 
      System.out.println("Please try again."); 
     } 
    } 
    else 
    { 
     System.out.println("Blue."); 
     System.out.println("Please try again."); 
    } 
    } 
} 

我有橙色和蓝色表示有一些输入错误,但它不完整的时间。我不知道如何处理这个问题,无论是循环还是for循环或try/catch。在学习Java时,我是新手,所以一些#注解在这方面会很有帮助。该代码旨在从字符串中读取用户中的三个整数。这很简单,但分析输入是我挣扎的地方。

+0

“代码旨在从用户字符串中读取三个用户整数的数字” - 您是否尝试提取用户输入的字符串中出现的前三个整数。前 - 如果输入是 - “1年去2名水手赢3美元”是你的程序应该提取1 2和3?或者输入应该是1(和)2(和)3? – opensam

+0

这对你来说可能有点高级,但是如果输入不正确,使用异常和异常处理来让事务死掉可能是一个很好的方法来处理这样的问题 – ControlAltDel

回答

0

本应该做的工作..

import java.util.Scanner; 

public class Main { 

public static void main(String[] args) { 

    int[] intArray = new int[3]; 
    Scanner keyboard = new Scanner(System.in); 

    for (int i = 0; i < intArray.length; i++) { 

     System.out.print("input integer #"+(i+1)+": "); 
     if (keyboard.hasNextInt()) { 
      intArray[i] = keyboard.nextInt(); 
      System.out.println("value for #"+(i+1)+": " + intArray[i]); 
     } else 
     { 
      System.out.println("not an integer"); 
      break; 
     } 

    } 

    keyboard.close(); 

} 

} 

也许是再次询问时数没有整数

import java.util.Scanner; 

public class Main2 { 

public static void main(String[] args) { 

    int[] intArray = new int[3]; 
    Scanner keyboard = new Scanner(System.in); 

    for (int i = 0; i < intArray.length; i++) { 

     boolean isInputCorrect = false; 
     do { 
      System.out.print("input integer #" + (i + 1) + ": "); 
      if (keyboard.hasNextInt()) { 
       intArray[i] = keyboard.nextInt(); 
       System.out.println("value for #" + (i + 1) + ": " + intArray[i]); 
       isInputCorrect = true; 
      } else { 
       System.out.println("not an integer, try again"); 
       keyboard.nextLine(); 
      } 

     } while (!isInputCorrect); 

    } 

    keyboard.close(); 

} 

} 
0

这并不意味着完全而是点回答你的问题的另一个解决方案你在正确的方向。这里应该有足够的帮助你弄清楚你的程序还需要什么。

import java.util.Scanner; 

public class Sandbox { 
    // arguments are passed using the text field below this editor 
    public static void main(String[] args) { 
    Scanner keyboard = new Scanner(System.in); 
    int num = 0; 
    System.out.println("enter an integer"); 

    while (true) { //keep prompting the user until they comply 
     try { 
      num = Integer.parseInt(keyboard.nextLine()); 
      keyboard.close(); 
      break; 
     } catch (NumberFormatException e) { 
      System.out.println("You must enter an integer"); 
     } 
    } 
    System.out.println("num: " + num); 
    } 
} 
0

我个人而言,会使用一个阵列的ArrayList(根据您的需求)来存储int类型,然后使用while循环检查和接受的数字。 while(array.length < 3)do {int checking}。这样,如果数组中已经有3个值,那么脚本将不会运行,并且会一直运行,直到您获得3个值。

+0

对于这个简单的例子,这可能有点高级。如果用户没有输入整数,OP似乎无法让他们的程序接受另一个输入。另外,OP看起来像是一个初学者。当他们在基本的程序控制中挣扎时,告诉他们添加数据结构并不是很有帮助。请记住,最简单的答案往往是最好的,特别是当有人第一次学习时。 – MusikPolice