2015-03-22 3 views
0

У меня проблема с входами в код. В программе я хочу создать меню, которое позволяет нам вставлять в него фигуры. В первом варианте, когда я пишу квадрат, круг или прямоугольник, программа должна задать мне сторону, радиус, высоту и ширину. Однако, когда я пишу тип формы, программа возвращается в меню, поскольку ничего не происходит. Не могли бы вы помочь?Входы не соответствуют

import java.util.*; 

    public class test 
    { 
    public static void main(String[] args) 
    { 
     Scanner scan = new Scanner(System.in); 
     Scanner scan2 = new Scanner(System.in); 
     String kindofshape; 
     int choice, radius, width, height, side; 

     do 
     { 
      System.out.println("(1) Add a shape to the list"); 
      System.out.println("(2) Get the total area of the shapes"); 
      System.out.println("(3) Show information of the shapes"); 
      System.out.println("(4) Quit"); 
      System.out.println("Choice : "); 
      choice = scan.nextInt(); 

      if (choice == 1) 
      { 
       System.out.println("enter the type of the shape"); 
       kindofshape = scan2.nextLine(); 
       if(kindofshape == "circle") 
       { 
        System.out.println("enter the radius"); 
        radius = scan.nextInt();    
       } 
       if(kindofshape == "rectangle") 
       { 
        System.out.println("enter the width"); 
        width = scan.nextInt(); 
        System.out.println("enter the height"); 
        height = scan.nextInt(); 
       } 
       if(kindofshape == "square") 
       { 
        System.out.println("enter the width"); 
        side = scan.nextInt(); 
       } 
      } 

     }while(choice != 4); 

     } 
    } 
+1

'==" круг "' [Как сравнить строки в Java?] (Http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Pshemo

ответ

2

Это Java. Вы должны сравнить строку с .equals(), но не с ==. Если вы сравниваете строки с ==, вы сравниваете экземпляры, но не строковые. И, пожалуйста, используйте switch против if-else конструкций.

+0

Я изменил и это работает. Благодарю. –

Смежные вопросы