I have this code:
import java.util.*;
public class Vormpjes {
public static void main(String[] args){
String vorm;
String nogmaals;
double diameter;
double basis;
double hoogte;
Scanner keyboard = new Scanner(System.in);
do {
System.out.println("Kies uit de 'cirkel', 'driehoek' of 'vierhoek':");
vorm = keyboard.nextLine();
if (vorm.equals("cirkel")){
System.out.println("Geef een diameter op (in cm)");
diameter = keyboard.nextDouble();
cirkel C1 = new cirkel();
C1.setDiam(diameter);
System.out.println("De diameter = " + C1.getDiam() + " cm");
System.out.println("De straal = " + C1.getRadius() + " cm");
System.out.println("De oppervlakte van de cirkel = " + C1.berekenOpp() + " cm2");
System.out.println("");
}
if (vorm.equals("driehoek")){
System.out.println("Geef de basis van de driehoek (in cm)");
basis = keyboard.nextDouble();
driehoek D1 = new driehoek();
D1.setBasis(basis);
System.out.println("Geef de hoogte van de driehoek (in cm)");
hoogte = keyboard.nextDouble();
D1.setHoogte(hoogte);
System.out.println("De oppervlakte = " + D1.berekenOppDriehoek() + " cm2");
System.out.println("");
}
System.out.println("Typ 'J' in als u het programma nogmaals wilt uitvoeren, druk anders op een andere wilekeurige toets.");
nogmaals = keyboard.nextLine();
}
while (nogmaals.equalsIgnoreCase("J"));
}
}
And the class 'cirkel' (that's how you spell circle in Dutch haha)
public class cirkel extends Vormpjes{
public double diam;
public double r;
//constructor
public cirkel(){
}
public double getDiam(){
return this.diam;
}
public void setDiam(double diam){
this.diam = diam;
}
public double getRadius(){
r = 0.5 * getDiam();
return this.r;
}
public double berekenOpp(){
return Math.PI * r * r;
}
public double berekenOmtrek(){
return 2 * Math.PI * r;
}
}
When the program asks the user if he wants to run the program again or not, I'm unable to type anything after while condition.
Why is it not working? I don't get errors or anything...
Jorjthe loop will continue. If you answer something else the program terminates, as expected, right?5<enter>and read a double, you still have<enter>in the to-read buffer. Last in your loop, you read a line (to getJor something else), and at this point you'll get the trailing part of the previous input that has not yet been read. The solution is to add anextLine()after each time you've read a double.