30. september 2008 - 12:16Der er
7 kommentarer og 1 løsning
parantes/else problemer
Hej.
Jeg har siddet og gloet mig blind på den her kode nu. Den siger fejl ved de to steder, der står else.
nogle der kan hjælpe? På forhånd tak.
Hilsen Michael
import java.util.Scanner;
public class AS44 {
/** * Author Michael Lunde */ public static void main(String[] args) {
int count=0; int vote; int voteObama=0; int voteMcCain=0;
Scanner keyboard = new Scanner(System.in);
do { System.out.println("Please vote for our next president."); System.out.println("1 -> McCain."); System.out.println("2 -> Obama."); System.out.println("0 -> Exit"); System.out.print("Please enter your vote: "); vote = keyboard.nextInt();
switch (vote) { case 1: System.out.println("You have put your vote on John McCain."); break; case 2: System.out.println("You have put yout vote on Barack Obama."); break; case 0: System.out.println("Total votes."); break; default: System.out.print("Invalid input vote! "); break; } } while (vote != 0); { if (vote = 2) count++; voteObama += 1; else System.out.println("Please enter 0, 1 or 2.");
if (vote = 1)
count++; voteMcCain += 1;
else System.out.println("Please enter 0, 1 or 2.");
}
System.out.println("John McCain has received "+voteMcCain+" votes."); System.out.println("Barack Obama has received "+voteObama+" votes.");
Ah ja.. det hjalp... men hvorfor vil den ikke tælle stemmerne sammen i voteMcCain og voteObama?
Min nye hele kode ser sådan her ud:
import java.util.Scanner;
public class AS43 {
/** * Author Michael Lunde. * In this program, you can enter sales ammounts, as long as they are not negative. * A negative number ends the program and tells the user the total sales amount, and also * how many total sales were made. */ public static void main(String[] args) {
int count=0; double salesAmount=0; double totalAmounts=1; double sum=0;
Scanner keyboard = new Scanner(System.in);
System.out.println();
System.out.println("Enter a sales ammount or a negative value to end. "); salesAmount = keyboard.nextDouble();
while (salesAmount >= 0) { count++; sum = sum + salesAmount;
System.out.println("Enter a sales ammount or a negative value to end. "); salesAmount = keyboard.nextDouble();
if (salesAmount>=0) { count++; totalAmounts += 1; } else { totalAmounts += 0; System.out.println("You have entered all sales ammounts.");
}
} System.out.printf("The total sales are %.2f.", sum); System.out.printf("The total amount of sales are "+ totalAmounts +"."); } }
Hvis du ikke sætter { } efter en if/else, så er det kun den næste linje der bliver betragtet som koden der skal eksekveres. Dvs. at din kode egenligt er:
if (vote = 2) { count++; }
voteObama += 1;
else { System.out.println("Please enter 0, 1 or 2."); }
if (vote = 1) { count++; }
voteMcCain += 1;
else { System.out.println("Please enter 0, 1 or 2."); }
Og så er det klart den brokker sig over at else ikke hører til noget. Du kan gøre som michael_stim siger, jeg har bare skrevet det lidt om: --> michael_stim: Dit kan så omskrives til :
switch( vote ) { case 1: voteMcCaion++; count++; break; case 2: voteObama++; count++; break; default: System.out.println("Please enter 0, 1 or 2."); }
/** * Author Michael Lunde */ public static void main(String[] args) {
int count=0; int vote; int voteObama=0; int voteMcCain=0;
Scanner keyboard = new Scanner(System.in);
do { System.out.println("Please vote for our next president."); System.out.println("1 -> McCain."); System.out.println("2 -> Obama."); System.out.println("0 -> Exit"); System.out.print("Please enter your vote: "); vote = keyboard.nextInt();
switch (vote) { case 1: System.out.println("You have put your vote on John McCain."); break; case 2: System.out.println("You have put yout vote on Barack Obama."); break; case 0: System.out.println("Total votes."); break; default: System.out.print("Invalid input vote! "); break; } } while (vote != 0); { if (vote == 2) { count++; voteObama += 1; } else if (vote == 1){ count++; voteMcCain += 1; } else { System.out.println("Please enter 0, 1 or 2."); }
Koden ser sådan her ud nu, men den vil stadig ikke tælle sammen?
import java.util.Scanner;
public class AS44 {
/** * Author Michael Lunde */ public static void main(String[] args) {
int count=0; int vote; int voteObama=0; int voteMcCain=0;
Scanner keyboard = new Scanner(System.in);
do { System.out.println("Please vote for our next president."); System.out.println("1 -> McCain."); System.out.println("2 -> Obama."); System.out.println("0 -> Exit"); System.out.print("Please enter your vote: "); vote = keyboard.nextInt();
switch (vote) { case 1: System.out.println("You have put your vote on John McCain."); count++; break; case 2: System.out.println("You have put yout vote on Barack Obama."); count++; break; case 0: System.out.println("Total votes."); break; default: System.out.print("Invalid input vote! "); break; } } while (vote != 0); { if (vote == 2) { count++; voteObama += 1; } else if (vote == 1){ count++; voteMcCain += 1; } else { System.out.println("Please enter 0, 1 or 2.");
}
System.out.println("John McCain has received " + voteMcCain + " votes."); System.out.println("Barack Obama has received " + voteObama + " votes."); }
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.