2017-01-13 3 views
0

У меня возникли проблемы с поиском не числа.только разрешает сканер java разрешать номера

Я пробовал try/catch, но я не могу схватить его. Если что-нибудь я получаю, чтобы улавливать не номера, но не позволяет пользователю снова пытаться войти ... Он просто полностью останавливает мой код.

Вот мой код:

package triangle; 

import java.util.Scanner; 

public class traingle { 

public static void main(String[] args){ 
    //explaining what the program is going to do 
System.out.println("You will be able to enter 3 angles to equal the sum of a triangle,\nthis automated program will tell you if the angle you entered is issosoliese, eqilateral or scalene"); 
//creating input 1,2,3 for user 

Scanner angle1 = new Scanner(System.in); // Reading from System.in 
System.out.println("Enter angle degree number 1: "); 
int n = angle1.nextInt(); 

Scanner angel2 = new Scanner(System.in); 
System.out.println("Enter angle degree number 2: "); 
int n2 = angel2.nextInt(); 

Scanner angel3 = new Scanner(System.in); 
System.out.println("Enter angle degree number 3: "); 
int n3 = angel3.nextInt(); 
//this is just telling how much degrees the user had in total if they didnt match up to triangle standards 
+3

Вам нужен только один «Сканер» *** и *** сегодня - действительно хороший день, чтобы узнать о * петлях * ** и ** * массивах *. Кроме того, почему пользователь вводит ** три ** значения? Подскажите для двух, а затем спросите, является ли третья правильная. –

+0

Считайте, что вы делаете то же самое 3 раза; сделайте это методом. Вам также необходимо иметь дело с недопустимым вводом; add loop, обеспечивает действительный ввод для каждого входа. Вам нужен 1 массив 'int' и 1 'Scanner'; добавьте цикл, который делает это для 3-х углов в массиве. – ChiefTwoPencils

+0

@ChiefTwoPencils вы могли бы привести мне пример? извините, что я давно не был из java –

ответ

0

Это должно вам начать.

package triangle; 

import java.util.Scanner; 

public class triangle { 

    public static void main(String[] args){ 
     //explaining what the program is going to do 
     System.out.println("You will be able to enter 3 angles to equal the sum of a triangle,\n"); 
     System.out.println("this automated program will tell you if the angle you entered is isoceles, eqilateral or scalene"); 
     //creating input 1,2,3 for user 

     Scanner s = new Scanner(System.in); // Reading from System.in 

     String line; 
     int [] angles = new int [3]; 
     int count = 0; 

     do { 
      System.out.print("Enter angle degree number " + (count+1) + ": "); 
      line = s.nextLine(); 
      while (true) { 
       try { 
        angles[count] = Integer.parseInt(line); 
        System.out.println("You entered " + angles[count]); 
        count++; 
        break; 
       } catch (NumberFormatException e) { 
        System.out.println("Invalid number: try again: "); 
        line = s.nextLine(); 
       } 
      } 
     } while (count < 3); 


    } 
} 
+0

Каковы будут переменные для каждого из углов? им жаль, что я не могу это понять –

+0

'angles [0]' - первое введенное значение, 'angles [1]' - второе введенное значение, а 'angles [2]' - третье введенное значение. –

-1

У вас есть простая проблема. Вы просто используете один сканер для кода в основном методе.

package triangle; 

import java.util.Scanner; 

public class traingle { 

public static void main(String[] argc){ 
System.out.println("You will be able to enter 3 angles to equal the sum of a triangle,\nthis automated program will tell you if the angle you entered is issosoliese, eqilateral or scalene"); 

Scanner angle1 = new Scanner(System.in); 

System.out.println("Enter angle degree number 1: "); 

int n = angle1.nextInt(); 

System.out.println("Enter angle degree number 2: "); 

int n2 = angel2.nextInt(); 

System.out.println("Enter angle degree number 3: "); 

int n3 = angel3.nextInt();