__________________
Senast redigerad av Proton 2021-02-22 kl. 11:13.
Senast redigerad av Proton 2021-02-22 kl. 11:13.
import java.util.Scanner;
public class HiLo {
public static void main(String[] args) {
System.out.println("Welcom to HiLo!");
System.out.println("Difficulty");
System.out.println("1. Easy (1-10)");
System.out.println("2. Medium (1-100)");
System.out.println("3. Hard (1-1000)");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
if (choice == 1) {
playGame(10);
System.out.println("Guess a number between 0-10.");
} else if (choice == 2) {
playGame(100);
System.out.println("Guess a number between 0-100.");
} else if (choice == 3) {
playGame(1000);
System.out.println("Guess a number between 0-1000.");
}
}
public static int playGame(int maxNumber) {
Scanner sc = new Scanner(System.in);
int number = (int)(Math.random() * maxNumber) + 1;
System.out.println("Guess i number between 1 - " + maxNumber);
int guess = -1;
while (guess != number) {
guess = sc.nextInt();
giveResponse(guess, number);
}
return guess;
}
public static void giveResponse(int guess, int answer) {
if (guess < answer) {
System.out.println("Too low, try agian!");
} else if (guess > answer) {
System.out.println("Too high, try agian!");
} else if (guess == answer) {
System.out.println("GOOD WORK.");
System.out.println("You guessed in" +----+ "attempts.");
}
}
}
int exempel = 0;
while(exempel < 1000) {
exempel += 1;
}
import java.util.Scanner;
public class HiLo {
public static void main(String[] args) {
System.out.println("Welcom to HiLo!");
System.out.println("Difficulty");
System.out.println("1. Easy (1-10)");
System.out.println("2. Medium (1-100)");
System.out.println("3. Hard (1-1000)");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
if (choice == 1) {
playGame(10);
System.out.println("Guess a number between 0-10.");
} else if (choice == 2) {
playGame(100);
System.out.println("Guess a number between 0-100.");
} else if (choice == 3) {
playGame(1000);
System.out.println("Guess a number between 0-1000.");
}
}
public static int playGame(int maxNumber) {
Scanner sc = new Scanner(System.in);
int number = (int)(Math.random() * maxNumber) + 1;
System.out.println("Guess i number between 1 - " + maxNumber);
int guess = -1;
while (guess != number) {
guess = sc.nextInt();
giveResponse(guess, number);
}
return guess;
}
public static void giveResponse(int guess, int answer) {
if (guess < answer) {
System.out.println("Too low, try agian!");
} else if (guess > answer) {
System.out.println("Too high, try agian!");
} else if (guess == answer) {
System.out.println("GOOD WORK.");
System.out.println("You guessed in" +----+ "attempts.");
}
}
}
int exempel = 0;
while(exempel < 1000) {
exempel += 1;
}
Citat:import java.util.Scanner; public class nextwork1 { public static void main(String[] args) { System.out.println("Welcom to HiLo!"); System.out.println("Difficulty"); System.out.println("1. Easy (1-10)"); System.out.println("2. Medium (1-100)"); System.out.println("3. Hard (1-1000)"); Scanner sc = new Scanner(System.in); int choice = sc.nextInt(); if (choice == 1) { playGame(10); System.out.println("Guess a number between 0-10."); } else if (choice == 2) { playGame(100); System.out.println("Guess a number between 0-100."); } else if (choice == 3) { playGame(1000); System.out.println("Guess a number between 0-1000."); } } public static int playGame(int maxNumber) { Scanner sc = new Scanner(System.in); int number = (int)(Math.random() * maxNumber) + 1; System.out.println("Guess i number between 1 - " + maxNumber); int guess = -1; int nrOfGuesses = 0; while (guess != number) { guess = sc.nextInt(); giveResponse(guess, number); } while(nrOfGuesses < 1000) { nrOfGuesses += 1; } return guess; } public static void giveResponse(int guess, int answer) { if (guess < answer) { System.out.println("Too low, try agian!"); } else if (guess > answer) { System.out.println("Too high, try agian!"); } else if (guess == answer) { System.out.println("GOOD WORK."); System.out.println("You guessed in "+nrOfGuesses+" attempts."); } } }
Citat:nextwork1.java:63: error: cannot find symbol System.out.println("You guessed in "+nrOfGuesses+" attempts."); ^ symbol: variable nrOfGuesses location: class nextwork1 1 error
import java.util.Scanner;
public class nextwork1 {
public static void main(String[] args) {
System.out.println("Welcom to HiLo!");
System.out.println("Difficulty");
System.out.println("1. Easy (1-10)");
System.out.println("2. Medium (1-100)");
System.out.println("3. Hard (1-1000)");
Scanner sc = new Scanner(System.in);
int choice = sc.nextInt();
if (choice == 1) {
playGame(10);
System.out.println("Guess a number between 0-10.");
} else if (choice == 2) {
playGame(100);
System.out.println("Guess a number between 0-100.");
} else if (choice == 3) {
playGame(1000);
System.out.println("Guess a number between 0-1000.");
}
}
public static int playGame(int maxNumber) {
Scanner sc = new Scanner(System.in);
int number = (int)(Math.random() * maxNumber) + 1;
System.out.println("Guess i number between 1 - " + maxNumber);
int guess = -1;
int nrOfGuesses = 0; // Deklarerad, här börjar 'nrOfGuesses' leva
while (guess != number) {
guess = sc.nextInt();
giveResponse(guess, number);
}
while(nrOfGuesses < 1000) {
nrOfGuesses += 1;
}
return guess;
// Här slutar 'nrOfGuesses' leva
}
public static void giveResponse(int guess, int answer) {
if (guess < answer) {
System.out.println("Too low, try agian!");
} else if (guess > answer) {
System.out.println("Too high, try agian!");
} else if (guess == answer) {
System.out.println("GOOD WORK.");
System.out.println("You guessed in "+nrOfGuesses+" attempts.");
}
}
}
while (guess != number) {
guess = sc.nextInt();
giveResponse(guess, number, nrOfGuesses);
}
public void a(){
int v1 = 0;
b();
System.out.println(v1);
}
public void b(){
int v1 = 1;
System.out.println(v1);
}
if(someCondition){
int a = 0;
//mer kod
}
System.out.println(a); //KOMPILERAR EJ, a är inte tillgängligt då den försvunnit ur scope med iffen
int a = 0;
if(condition){
a = 1;
}
System.out.println(a); //OK, a är deklarerad i ett scope som fortfarande finns här.
Flashback finansieras genom donationer från våra medlemmar och besökare. Det är med hjälp av dig vi kan fortsätta erbjuda en fri samhällsdebatt. Tack för ditt stöd!
Swish: 123 536 99 96 Bankgiro: 211-4106
Flashback finansieras genom donationer från våra medlemmar och besökare. Det är med hjälp av dig vi kan fortsätta erbjuda en fri samhällsdebatt. Tack för ditt stöd!
Swish: 123 536 99 96 Bankgiro: 211-4106