2017-11-16 9 views
0

次の場合、ユーザー入力を(日)からどのように使用するのかを理解するのが難しく、最後の行でそれを利用して時間と分を計算しますユーザー入力(日)。後で別の方法でユーザー入力を利用する方法

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.print("Great! "+destination +" sounds like a great trip"); 
    } 
    public static void 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(); 
    } 
    public static void third() { 
     String days; 
     String hours = days * 24; 
     String minutes = hours * 60; 

     System.out.println("If your are travelling for " + days + " days that is the same as " + hours + " hours or " + minutes + " minutes"); 
    } 
} 
+0

現在行っているときはどうなりますか?エラーが発生した場合は、エラーメッセージを共有してください。 – Antimony

+0

エラー:(36,28)java: '.class'が予想される エラー:(36,33)java:ステートメントではない*行36:String hours = days * 24; –

答えて

0

は、あなたのnext()メソッドの戻り値String、空ではないことを確認します。

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; 
} 

そしてあなた third()メソッドで次のコード行に変更:以下とのコードも

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

変更と

String days; 

を次の2行:

int hours = days * 24; 
int minutes = hours * 60; 
+0

それを修正しました!ありがとうございました!もう1つの質問です。「String hours = days * 24;」それは今、次のようなエラーを出しています:java:バイナリ演算子のための悪いオペランドの型 "*"第1の型:java.lang.String第2の型:int ++設定した方法で単純な数学的なエラーがあると仮定していますコード行? –

+0

今回はエラーなしのラン!ただし、最後に進む前にnext()メソッドを繰り返しました。 –

0

まず、プログラムが正常にコンパイルされるべきではないようです。 third()内のオブジェクト 'days'はString型です。 Stringオブジェクトに数値24を掛けることはできません。次の行の分および時間についても同様です。

また、 'days'はnext()で受け入れられているため、ローカル変数です。それは3番目に何の意味も持たない()。

おそらくこれが役立つ可能性があります。

import java.util.Scanner; 

public class LearnScanner 
{ 
    static String days = null; 

    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.print("Great! " + destination + " sounds like a great trip"); 
    } 

    public static void next() 
    { 

     Scanner input = new Scanner(System.in); 
     System.out.print("How many days are you going to spend travelling?"); 
     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(); 
    } 

    public static void third() 
    { 

     String hours = String.valueOf(Integer.valueOf(days) * 24); 
     String minutes = String.valueOf(Integer.valueOf(hours) * 60); 

     System.out.println(
       "If your are travelling for " + days + " days that is the same as " 
         + hours + " hours or " + minutes + " minutes"); 
    } 

} 
+0

ありがとう!これは今私にとって論理的なようです。ただし、提案された編集を行った後、私は次のエラーが発生します:java:シンボルシンボルを見つけることができません:変数日場所:class LearnScanner –

関連する問題