import java.util.Scanner;
public class Formula {
public static void main(String[] args) {
Scanner numIn = new Scanner(System.in);
Scanner form = new Scanner(System.in);
double r, d, h, a;
String formula;
System.out.println("Please state which circle formula you want to use:");
System.out.println("Circumference");
System.out.println("Area");
System.out.println("Cylinder volume");
formula = form.next();
switch (formula) {
case "Circumference":
System.out.println("Please state the diameter: ");
d = numIn.nextDouble();
System.out.println("The circumference is:");
System.out.println(3.14 * d);
break;
case "Area":
System.out.println("Please state the radius: ");
r = numIn.nextDouble();
System.out.println("The area is:");
System.out.println(3.14 * (r * r));
break;
case "Cylinder volume":
System.out.println("State the area of the base: ");
a = numIn.nextDouble();
System.out.println("State the height of the cylinder: ");
h = numIn.nextDouble();
System.out.println("the volume is: ");
System.out.println(a * h);
break;
default:
System.out.println("Option not recognized");
break;
}
}
}
私は数式計算機を作成しようとしています(注:私は初心者です)、最後の「ケース」まではすべて動作しているようです。最後のケース「シリンダーボリューム」はコンソールに入力すると認識されません。他のすべてのケースはうまく動作し、 "シリンダーボリューム"と他のものとの違いはありません。助けてください!なぜコードは機能しませんか?
'form.next()'の代わりに 'form.nextLine()'を使うと、最初の単語( "Cylinder")しか得られません。 – Zircon
* "なぜコードが機能しないのですか" *は悪いタイトルです。おそらくSOに関する質問の90%以上に当てはまるかもしれません。あなたはより具体的になりたいかもしれません。 – domsson
あなたは[next()とnextLine()の違いを知る必要があると思います(http://stackoverflow.com/questions/22458575/whats-the-difference-between-next-and-nextline-methods-from-スキャナクラス)。将来的には、[ドキュメントを読んでください](https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html) –