1つの数字を入力するし表示するプログラムはできたのですが複数を入力するにはどこを変更すればよいのでしょうか
public class Main{ public static void main(String[] args){ Input in = new Input(); double n = in.inputDouble("実数を入力してください:"); System.out.println("入力された実数 "+ n +"です。"); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Input{ private String prompt; private final String ERROR_INPUT_DOUBLE = "入力値は実数ではありません。"; private final String ERROR_INPUT = "エラー:入力に誤りがあります。"; private void setPrompt(String prompt){ this.prompt = prompt; } private String input() throws IOException{ String line; BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); line = reader.readLine(); return line; } public double inputDouble(String prompt){ this.setPrompt(prompt); System.out.println(this.prompt); try{ String value = this.input(); double n = Double.parseDouble(value); return n; }catch(Exception s){ System.out.println(ERROR_INPUT_DOUBLE); return this.inputDouble(prompt); } } public String inputString(String prompt){ this.setPrompt(prompt); System.out.println(this.prompt); try{ String n = this.input(); return n; }catch(Exception s){ System.out.println(ERROR_INPUT); return this.inputString(prompt); } } }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。