This question already has an answer here:
- How do I compare strings in Java? 23 answers
My code is here:
import java.util.Random;
import java.util.Scanner;
public class cylindersVolumeAndSArea {
public static void main(String[] args) {
Random chooser = new Random();
Scanner sc = new Scanner(System.in);
double pi = 3.14;
int r = chooser.nextInt(10);
int h = chooser.nextInt(20);
if (2 >= r) {
r = r + 2;
} else {
}
if (2 >= h) {
h = h + 2;
} else {
}
double er = r * r; // exponential (to the power of 2) for example
double sav1 = 2 * pi * r;
double sav2 = 2 * pi * er * h;
double surArea = sav1 + sav2;
double ap = 2 * pi;
double ne = ap * r;
double na = ap * er;
double lo = na * h;
String c;
System.out.println("Surface area or Volume");
c = sc.next();
if (c == "Surface area") {
} else {
System.out.println("ok, surface area coming right up, just to confirm, say sa:");
}
double ra = chooser.nextInt(10);
double ha = chooser.nextInt(20);
if (2 >= ra) {
ra = ra + 2;
} else {
}
if (2 >= ha) {
ha = ha + 2;
} else {
}
double era = ra * ra;
double Volume = 2 * pi * era * ha;
if (c == "Volume") {
} else {
System.out.println("ok, volume coming right up, just to confirm, say v");
}
String a;
a = sc.next();
if (a == "sa") {
} else {
System.out.println("Thank you for confirming, sur area is right down there.");
System.out.println("The answer to a cylinder's surface area: " + surArea);
System.out.println("The math:");
System.out.println("r = " + r + ", h = " + h + ", er = " + er);
System.out.println("sav1 = " + sav1 + ", 2 * pi = " + ap + " * r = " + ne);
System.out.println("sav2 = " + sav2 + ", 2 * pi = " + ap + ", * er = " + na + ", * h = " + lo + ", and + sav1 = " + surArea);
}
String v;
v = sc.next();
if (v == "v") {
} else {
System.out.println("Thank you for confirming, Volume is right down there.");
}
}
}
When I compile, I get what I want to start:
Surface area or Volume
And now comes the crazy part I can't figure out is wrong:
if I say Surface area, i get:
Surface area
ok, surface area coming right up, just to confirm, say sa:
ok, volume coming right up, just to confirm, say v
Thank you for confirming, sur area is right down there.
The answer to a cylinder's surface area: 87.92
The math:
r = 2, h = 3, er = 4.0
sav1 = 12.56, 2 * pi = 6.28 * r = 12.56
sav2 = 75.36, 2 * pi = 6.28, * er = 25.12, * h = 75.36, and + sav1 = 87.92
And if I say Volume, i get:
Volume
ok, surface area coming right up, just to confirm, say sa:
ok, volume coming right up, just to confirm, say v
v
Thank you for confirming, sur area is right down there.
The answer to a cylinder's surface area: 1657.92
The math:
r = 8, h = 4, er = 64.0
sav1 = 50.24, 2 * pi = 6.28 * r = 50.24
sav2 = 1607.68, 2 * pi = 6.28, * er = 401.92, * h = 1607.68, and + sav1 = 1657.92
I am new to scanners, but I can't figure out where I went wrong and how to fix it.
BTW: what this is, is a Surface area or Volume finder; of a cylinder. (For anyone interested).
What I would like to see happen is this:
Surface area or Volume
Then if I said Surface area, this would come up:
Surface area
ok, to confirm, just say sa
And then I would say sa and this would pop up:
Thank you for confirming, sur area is right down there.
The answer to a cylinder's surface area: 1657.92
The math:
r = 8, h = 4, er = 64.0
sav1 = 50.24, 2 * pi = 6.28 * r = 50.24
sav2 = 1607.68, 2 * pi = 6.28, * er = 401.92, * h = 1607.68, and + sav1 = 1657.92
Does that answer your question?
Aucun commentaire:
Enregistrer un commentaire