2016-07-10 13 views
1

Ich versuche, Benutzereingaben in meine Klasse hinzuzufügen, so dass der Benutzer eingeben kann, wie viel sein Paket wiegt und meine Klasse wird ihnen sagen, wie viel es wird sie kosten. Ich bin sehr neu in Java, also weiß ich, das ist nicht erstaunlich, aber es ist das Beste, was ich zu diesem Zeitpunkt tun könnte. Vielen Dank im VorausWie füge ich Benutzereingaben in mein Java-Programm zur Verwendung in if-Anweisungen hinzu?

public class ShippingCosts { 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     int weight = 0; 

     if (0 < weight && weight <= 1) 
      System.out.println("The cost to ship the package is $3.50"); 
     if (1 < weight && weight <= 3) 
      System.out.println("The cost to ship the package is $5.50"); 
     if (3 < weight && weight <= 10) 
      System.out.println("The cost to ship the package is $9.50"); 
     if (10 < weight && weight <= 20) 
      System.out.println("The cost to ship the package is $13.50"); 
     if (20 < weight) 
      System.out.println("The package is too heavy to be shipped"); 

     System.out.println("How heavy is the package?"); 

    } 

} 
+0

Bitte suchen Sie Stack-Überlauf vor dem Posten. –

Antwort

1

einfaches Beispiel soll dies sein: versuchen, mehr darüber in der Dokumentation Java Scanner

// read a number from System.in: 
    Scanner scan = new Scanner(System.in); 
    String s = scan.next(); 
    int i = scan.nextInt(); //read integers 

> public String next() --->it returns the next token from the scanner. 

> public String nextLine()---> it moves the scanner position to the next line and returns the value as a string. 

> public byte nextByte() --->it scans the next token as a byte. 

> public short nextShort() --->it scans the next token as a short value. 

> public int nextInt() --->it scans the next token as an int value. 

> public long nextLong() --->it scans the next token as a long value. 

> public float nextFloat() --->it scans the next token as a float value. 

> public double nextDouble() --->it scans the next token as a double value. 

den Benutzer zur Eingabe lesen:

Scanner input = new Scanner(System.in); 

System.out.println("Please enter your name : "); //prompt user 
s = input.next(); // getting a String value 

System.out.println("Please enter your age : "); 
i = input.nextInt(); // getting an integer 

System.out.println("Please enter your salary : "); 
d = input.nextDouble(); // getting a double 
+0

Also habe ich das in meinen Code eingefügt und den java.util.Scanner importiert, aber ich bin immer noch ziemlich verwirrt darüber, wie ich von hier aus tatsächlich dazu in der Lage bin, Benutzereingaben zu bekommen. Als ich sagte, dass ich neu in Java bin, meine ich, als ob ich vor ein paar Tagen angefangen hätte, also wird es eine Weile dauern, bis es mir besser geht. – cpitt6477

+0

Danke für die Aufklärung! – cpitt6477