2017-11-16 3 views
0

Ich habe die Fehler in meiner ursprünglichen Code festgelegt und es richtig formatiert. Jedoch wird der Code nun Wiederholen String next(), bevor es an das letzte Verfahren geht. Ich dachte, ich verstand, warum, aber als ich versuchte, es zu beheben, das Programm nicht wieder. Vielen Dank für Ihre Zeit!Aktualisiert util.scanner Wiederholung Methode

import java.util.Scanner; 

public class LearnScanner { 
    public static void main (String[] args) { 
     first(); 
     next(); 
     third(); 
    } 
    public static void first() { 
     Scanner input = new Scanner(System.in); 
     System.out.println("Welcome to Vacation Planner!!"); 
     System.out.print("What is your name?"); 
     String name = input.nextLine(); 
     System.out.print("Nice to meet you " + name + ", where are you travelling to?"); 
     String destination = input.nextLine(); 
     System.out.println("Great! "+destination +" sounds like a great trip"); 
    } 
    public static String next() { 
     Scanner input = new Scanner(System.in); 
     System.out.print("How many days are you going to spend travelling?"); 
     String days = input.nextLine(); 
     System.out.print("How much money in USD are you planning to spend on your trip?"); 
     String budget = input.nextLine(); 
     System.out.print("What is the three letter currency symbol for your travel destination?"); 
     String currency = input.nextLine(); 
     System.out.print("How many " + currency + " are there in 1 USD?"); 
     String currencyConversion = input.nextLine(); 
     return days; 
    } 
    public static void third() { 
     int days = Integer.valueOf(next()); 
     int hours = days * 24; 
     int minutes = hours * 60; 
     System.out.println("If your are travelling for " + days + " days that is the same as " + hours + " hours or " + minutes + " minutes"); 
    } 


    } 

Antwort

1

Von dem, was ich sehe, das Verfahren next() einmal von der Haupt Methode aufgerufen wird, und dann wieder in dritten aufgerufen wird bei

int days = Integer.valueOf(next()); 

Vielleicht sollten Sie eine Instanzvariable namens Tagen erstellen und Speichern Sie den Wert von next() darin. Dann, um den Wert der Variable in der dritten() Methode. dh

import java.util.Scanner; 

public class LearnScanner { 
    private static int days = 0; 

    public static void main (String[] args) { 
     first(); 
     days = Integer.parseInt(next()); 
     third(); 
    } 
    public static void first() { 
     Scanner input = new Scanner(System.in); 
     System.out.println("Welcome to Vacation Planner!!"); 
     System.out.print("What is your name?"); 
     String name = input.nextLine(); 
     System.out.print("Nice to meet you " + name + ", where are you travelling to?"); 
     String destination = input.nextLine(); 
     System.out.println("Great! "+destination +" sounds like a great trip"); 
    } 
    public static String next() { 
     Scanner input = new Scanner(System.in); 
     System.out.print("How many days are you going to spend travelling?"); 
     String days = input.nextLine(); 
     System.out.print("How much money in USD are you planning to spend on your trip?"); 
     String budget = input.nextLine(); 
     System.out.print("What is the three letter currency symbol for your travel destination?"); 
     String currency = input.nextLine(); 
     System.out.print("How many " + currency + " are there in 1 USD?"); 
     String currencyConversion = input.nextLine(); 
     return days; 
    } 
    public static void third() { 
     int tempDays = Integer.valueOf(days); 
     int hours = days * 24; 
     int minutes = hours * 60; 
     System.out.println("If your are travelling for " + tempDays + " days that is the same as " + hours + " hours or " + minutes + " minutes"); 
    } 
+0

Das ist die richtige Antwort – mvreijn

+0

Nach der Anwendung dieser Änderung, die ich die folgende Fehlermeldung erhalten: java: inkompatible Typen: java.lang.String kann nicht in int –

+0

umgewandelt werden ich, dass .. Sie die Zeichenfolge konvertieren sollte Ganze Zahl. Ich werde den Code jetzt –

Verwandte Themen