単純な足し算プログラムの書き換えができません。
System.out.printではなく、System.out.writeを使用するプログラムに書き換えたいのですが、
うまくいきません。どのようにすればよいでしょうか。
★書き換え前(System.out.printを使用)★
import java.util.Scanner;
public class Sample{
public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("number1="); int num1 = s.nextInt(); System.out.print("number2="); int num2 = s.nextInt(); System.out.print(num1 + num2); }
}
☆書き換え後(System.out.writeを使用)☆
public class Sample{
//mainメソッドの文字を出力 static void syuturyoku(char[] str) { int i = 0; while (i < str.length) { System.out.write(str[i]); i++; } } public static void main(String[] args) throws Exception{ char[] num1 = "num1=".toCharArray(); char[] num2 = "num2=".toCharArray(); syuturyoku(num1); System.out.write('\n'); int num1 = System.in.read()-'0'; System.out.write('\n'); syuturyoku(num2); System.out.write('\n'); int num2 = System.in.read()-'0'; System.out.write('\n'); /*計算結果の出力方法がわかりません*/ }
}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。