I started a project to understand deeply nested loops and classes. In my CYCLING method when I reach the if(y >= 0) loop it doesn't properly use my variables in classes. For example if MPH is 15 and the gear is 1 or 3 it won't ask me to change gear. Or if gear is 1 and speed is 11+ it wont ask me to change gear? What am I doing incorrectly?
public class Bike {
int speed;
int gear;
void changeGear(int newVal) {
gear = newVal;
}
void speedUp(int newVal) {
speed = newVal;
}
void breaks(int slow) {
speed = speed + slow;
}
void printState() {
System.out.println("Gear is: " + gear);
System.out.println("Speed is: " + speed + ("MPH"));
}
}
//________________________
public static boolean cycle = true;
public static Bike b1 = new Bike();
public static int x;
public static int y;
public static Scanner input = new Scanner(System.in);
public static int choice;
//______________________________
public static void cycling() {
while (cycle == true) {
System.out.println("What would you like to do now? Enter a number.");
System.out.println("1: Speed Change");
System.out.println("2: Change Gear");
choice = input.nextInt();
if (choice == 1) {
System.out.println("Choose a speed change");
y = input.nextInt();
if (y < 0) {
b1.breaks(y);
if (b1.speed < 0) {
b1.speed = Math.abs(y) + y;
System.out.println("You've stopped entirely");
}
}
if (y >= 0) {
b1.speed = y;
b1.printState();
if (b1.speed >= 0 && b1.speed <= 10) {
while (b1.gear != 1) {
System.out.println("You need to be in Gear 1 for " + "that! Please change gears.");
x = input.nextInt();
b1.changeGear(x);
}
if (b1.speed >= 11 && b1.speed <= 20) {
while (b1.gear != 2) {
System.out.println("You need to be in Gear 2 for" + "that! Please change gears.");
x = input.nextInt();
b1.changeGear(x);
}
if (b1.speed >= 21) {
while (b1.gear != 3) {
System.out.println("You need to be in Gear 3 for" + "that! Please change gears.");
x = input.nextInt();
b1.changeGear(x);
}
}
/*if(b1.speed >= 0 && b1.speed <=10){
b1.gear = 1;
}else if(b1.speed >= 11 && b1.speed <=20){
b1.gear = 2;
}else if(b1.speed >= 21){
b1.gear = 3;
}*/
b1.printState();
}
}
}
}
}
}
Think about your statements. You have something that basically looks like this:
if (b1.speed >= 0 && b1.speed <= 10) {
//some while loop here to do whatever
if (b1.speed >= 11 && b1.speed <= 20) {
//more code
}
}
In your code, this statement will never be true:
if (b1.speed >= 11 && b1.speed <= 20) {
The only way you get to that statement is if b1.speed>=0 && b1.speed<=10. Therefore, will b1.speed ever be between 11 and 20 when you get to that second (nested) if statement?
Related
boolean onGoing = true;
do {
String p1 = "playing";
while (p1.equals("playing")) {
System.out.println("Player 1, enter hit row/column:");
int a = sc.nextInt();
int b = sc.nextInt();
if (a >= 0 && a < 5 && b >= 0 && b < 5) {
if (history1[a][b] == '-') {
p1 += " no more";
if (board2[a][b] == '#') {
history1[a][b] = 'X';
board2[a][b] = 'X';
String status = check(board2);
if (status.equals("win")) {
System.out.println("PLAYER 1 WINS! YOU SUNK ALL OF YOUR OPPONENT'S SHIPS!");
onGoing = false;
printBattleShip(board1);
printBattleShip(board2);
break;
} else {
System.out.println("PLAYER 1 HIT PLAYER 2's SHIP!");
}
} else if (board2[a][b] == '-') {
System.out.println("PLAYER 1 MISSED!");
history1[a][b] = 'O';
board2[a][b] = 'O';
}
} else {
System.out.println("You already fired on this spot. Choose different coordinates.");
}
} else {
System.out.println("Invalid coordinates. Choose different coordinates.");
}
}
String p2 = "playing";
while (p2.equals("playing")) {
System.out.println("Player 2, enter hit row/column:");
int c = sc.nextInt();
int d = sc.nextInt();
if (c >= 0 && c < 5 && d >= 0 && d < 5) {
if (history2[c][d] == '-') {
p2 += " no more";
if (board1[c][d] == '#') {
history2[c][d] = 'X';
board1[c][d] = 'X';
String status = check(board1);
if (status.equals("win")) {
System.out.println("PLAYER 2 WINS! YOU SUNK ALL OF YOUR OPPONENT'S SHIPS!");
onGoing = false;
printBattleShip(board2);
printBattleShip(board1);
break;
} else {
System.out.println("PLAYER 2 HIT PLAYER 1's SHIP!");
}
} else if (board1[c][d] == '-') {
history2[c][d] = 'O';
board1[c][d] = 'O';
System.out.println("PLAYER 2 MISSED!");
}
} else {
System.out.println("You already fired on this spot. Choose different coordinates.");
}
} else {
System.out.println("Invalid coordinates. Choose different coordinates.");
}
}
} while (onGoing);
private static String check(char[][] arr1) {
int sum = 0;
for (int i = 0; i < arr1.length; i++) {
for (int j = 0; j < arr1.length; j++) {
if (arr1[i][j] == '#') {
sum += 1;
}
}
}
if (sum == 0) {
return "win";
} else {
return "keep playing";
}
}
// I declared and initialized a boolean variable called ongoing before a do-while loop. Then I create a while loop inside the do while loop. And inside the while loop, there are a series of conditional statement. Eventually, I changed the value of the boolean variable and feed it back to the while statement, letting it evaluate it. But it doesn't seem to change the value. Is it because the scope of the variable? How can I fix it?
edit: check method added
Currently I'm trying to implement heuristics for a 3D tic-tac-toe but it seems like my counter is way of it,f but I'm unsure where I've done wrong, I'm not gonna post all of the code since it's a lot, but here is a part:
public void countDiagonal(GameState gameState) {
/*
* yz-plane (negativ)
*/
int z;
for (int x = 0; x < GameState.BOARD_SIZE; x++) {
for (int y = 0; y < GameState.BOARD_SIZE; y++) {
z = y;
if (gameState.at(x, y, z) == myPlayer) {
myCounter++;
opponentCounter = 0;
}
if (gameState.at(x, y, z) == opponentPlayer) {
opponentCounter++;
myCounter = 0;
}
if (gameState.at(x, y, z) == Constants.CELL_EMPTY) {
emptyCells++;
}
}
evaluateBoard();
myCounter = 0;
opponentCounter = 0;
emptyCells = 0;
}
The evaluation is done here:
public void evaluateBoard() {
if (myCounter == 1 && emptyCells == 3) {
myScore = myScore + 5;
}
if (myCounter == 2 && emptyCells == 2) {
myScore = myScore + 10;
}
if (myCounter == 3 && emptyCells == 1) {
myScore = myScore + 20;
}
if (myCounter == 4) {
myScore = myScore + 1000;
}
if (opponentCounter == 1 && emptyCells == 3) {
opponentScore = opponentScore + 5;
}
if (opponentCounter == 2 && emptyCells == 2) {
opponentScore = opponentScore + 10;
}
if (opponentCounter == 3 && emptyCells == 1) {
opponentScore = opponentScore + 20;
}
if (opponentCounter == 4) {
opponentScore = opponentScore + 1000;
}
}
When I try to run it, I use alpha-beta prune, but it seems like the calculation are done horrbly wrong, when I use the value, I take myScore - opponentScore and I use an alpha-beta tree with depth 1, but even after only playing one move, I'm down -15 in points, as I'm a noob in java, im therefore asking for help, is there an obvious mistake in my way of trying to evaluate the board?
I'm making a simple "Whack a mole" game in Java. For simplicity I have created a 10*10 box and placed 10 moles in random boxes. I want to exit the game when the user spent his 50 inputs or found all 10 moles, but there seems to be a problem in terminating the while loop even when the user attempts specified inputs.
Is it Instance variable scope problem? Why it is not working?
public class WhackAMole {
int score = 0, molesLeft = 10, attempts;
char[][] moleGrid = new char[10][10];
int numAttempts, gridDimension;
public WhackAMole(int numAttempts, int gridDimension) {
// TODO Auto-generated constructor stub
this.numAttempts = numAttempts;
this.gridDimension = gridDimension;
}
boolean place(int x, int y) {
return (x == 2 && y == 5)
|| (x == 1 && y == 3)
|| (x == 8 && y == 4)
|| (x == 5 && y == 10)
|| (x == 6 && y == 9)
|| (x == 10 && y == 7)
|| (x == 3 && y == 7)
|| (x == 2 && y == 9)
|| (x == 4 && y == 8)
|| (x == 9 && y == 5);
}
void whack(int x, int y) {
if (place(x, y)) {
if (moleGrid[x - 1][y - 1] == 'W') {
System.out.println("Already attempted! \'try other co-ordinates\' \n");
} else {
moleGrid[x - 1][y - 1] = 'W';
this.score ++;
this.molesLeft --;
}
}
}
void printGridToUser() {
System.out.println("your score is " + score + " and " + molesLeft + " moles are left. \n");
System.out.println("input x = -1 and y = -1 to quit the game! \n");
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
System.out.print(" " + moleGrid[i][j] + " ");
}
System.out.println("\n");
}
}
void printGrid() {
for(int i = 0; i < 10; i++){
for(int j = 0; j < 10; j++){
this.moleGrid[i][j] = '*';
}
}
}
public static void main(String[] args) {
WhackAMole game;
System.out.println("Lets play the Whack A Mole!\n");
game = new WhackAMole(50, 100);
game.printGrid();
game.printGridToUser();
Scanner scanner = new Scanner(System.in);
while ((game.numAttempts > 0) || (game.molesLeft > 0)) {
System.out.println("Enter box co-ordinate\n");
System.out.println("x co-ordinate: \n");
int x = scanner.nextInt();
System.out.println("y co-ordinate: \n");
int y = scanner.nextInt();
if (x == -1 && y == -1) {
break;
} else if ((x < 1 || y < 1) || (x > 10 || y > 10)) {
System.out.println("please enter values of x and y greater than 0 and less than 11! \n");
} else {
game.whack(x, y);
game.numAttempts--;
game.gridDimension--;
System.out.println("you can have upto " + game.numAttempts + " out of " + game.gridDimension + " boxes \n");
game.printGridToUser();
}
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
if (game.place(i+1, j+1) && game.moleGrid[i][j] != 'W'){
game.moleGrid[i][j] = 'M';
}
}
}
game.printGridToUser();
scanner.close();
System.out.println("game over!!!\n");
}
}
Your while loop is not ending because you are using || in your while loop. The || is making your loop run until the attempts allowed i.e. 50 and the right guessing i.e. finding moles correct both are met. So even when a gamer has finished his allowed attempts and hasn't guessed all the right moles positions, the loop will not end
The simple solution would be to replace || with &&
while ((game.numAttempts > 0) && (game.molesLeft > 0))
And avoid using fixed numbers i.e. 10 in your for loops instead use
for (int i = 0; i < game.gridDimension; i++) {
for (int j = 0; j < game.gridDimension; j++) {
I hope it helps
Your loop is using an or for the test function. This means both condition mist be false in order for it to stop. In your case. How its written you must exhaust the numtries and have no moles left.
Change to use && vs ||.
I've written a simple Java program to display the results of 20 dice rolls on the console. The results I'm getting are listed below:
3
1
java.util.Random#62efae3b
1
5
4
1
java.util.Random#62efae3b
1
java.util.Random#62efae3b
java.util.Random#62efae3b
1
6
java.util.Random#62efae3b
1
java.util.Random#62efae3b
java.util.Random#62efae3b
1
2
3
3
When I ran it for a few times, the string after "#" is different, but basically in the same format. What have I done wrong?
Here is the code:
import java.util.Random;
public class QiProb3 {
public static void main(String[] args) {
Random diceNumber = new Random();
for (int count = 0; count <= 20; count++) {
if ((diceNumber.nextInt(6) + 1) == 1) {
System.out.println("1");
} else if ((diceNumber.nextInt(6) + 1) == 2) {
System.out.println("2");
} else if ((diceNumber.nextInt(6) + 1) == 3) {
System.out.println("3");
} else if ((diceNumber.nextInt(6) + 1) == 4) {
System.out.println("4");
} else if ((diceNumber.nextInt(6) + 1) == 5) {
System.out.println("5");
} else if ((diceNumber.nextInt(6) + 1) == 6) {
System.out.println("6");
} else {
System.out.println(diceNumber);
}
}
}
}
else {
System.out.println(diceNumber);
}
You are printing the address of diceNumber by invoking its default toString() function in your else clause.
That is why you are getting the java.util.Random#62efae3b
The more critical issue is why it gets to the 'else' clause, I believe that is not your intention.
Note: In the question, a new number is generated in each if/else if clause, which is why the code actually gets to the final else clause.
What you should be doing is:
for (int count = 0; count < 20; count++) {
int rollValue = diceNumber.nextInt(6) + 1;
if (rollValue == 1) {
System.out.println("1");
} else if (rollValue == 2) {
System.out.println("2");
} else if (rollValue == 3) {
System.out.println("3");
} else if (rollValue == 4) {
System.out.println("4");
} else if (rollValue == 5) {
System.out.println("5");
} else if (rollValue == 6) {
System.out.println("6");
} else {
// This else is now redundant
System.out.println(diceNumber);
}
}
or a more straight-forward method would be:
// count < 20 instead of count <= 20
for (int count = 0; count < 20; count++) {
int rollValue = diceNumber.nextInt(6) + 1;
System.out.println(rollValue);
}
Credit goes to 'Elliott Frisch' for realizing that the loop is
executed 21 times instead of 20.
You're Re-Rolling
With each if you re-roll the dice. Store the value, and test it!
Random diceNumber = new Random();
for (int count = 0; count <= 20; count++) {
int roll = diceNumber.nextInt(6) + 1;
if (roll == 1) {
System.out.println("1");
} else if (roll == 2) {
System.out.println("2");
} else if (roll == 3) {
System.out.println("3");
} else if (roll == 4) {
System.out.println("4");
} else if (roll == 5) {
System.out.println("5");
} else if (roll == 6) {
System.out.println("6");
} else {
System.out.println("RNG Error: " + diceNumber);
}
}
It Could be More Concise
Your posted code might be shortened like
for (int count = 0; count <= 20; count++) {
int roll = diceNumber.nextInt(6) + 1;
System.out.println(roll);
}
Note
Also, you get 21 rolls using the above <= 20 test.
You can do this without the large if-else ladder:
int x = 0;
int i = 0;
while(i < 20){
x = (int)(Math.random() * 7);
if(x != 0)
{
System.out.println((int)Math.floor(x));
i++;
}
}
Math.random() gets a value between 0 and 1 and this value is multiplied to 7. If the dice turns out to be zero, skip the roll and do another one. The Math.floor() value will round the decimal value down to the nearest integer (if product = 6.2 then the output of the roll will be 6).
Please help with formatting my output.
I have been asked to "Write a program that displays all the leap years, ten per line, in the twenty-first century (from 2001 to 2100), separated by exactly one space".
Although I get the right results, it's not in the required format.
Thanks in advance
public class Leapyear {
public static void main(String[] args) {
//declare variabls;
int year;
int count=1;
int yearsperline = 10;
//loop
for(year=2001;2001<=2100;year++){
if((year%4==0 && year%100!=0) || (year%400==0))
System.out.print(year+",");
if ( year ==2100)
break;
while (count%10==0)
System.out.println();
}
}
}
You can write something like this:
//declare variables;
final int YEARS_PER_LINE = 10;
final int START_YEAR = 2001;
//loop
for(int year = START_YEAR; year <= 2100; year++){
System.out.print(year);
if((year - START_YEAR) % YEARS_PER_LINE == (YEARS_PER_LINE - 1)) {
System.out.println();
} else {
System.out.print(",");
}
}
Try this one:
public class LeapYear
{
public static void main(String[] args)
{
// declare variables
final int startYear = 2001;
final int endYear = 2100;
final int yearsPerLine = 10;
// loop
for (int year = startYear, count = 0; year <= endYear; year++)
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
if (count % yearsPerLine != 0)
System.out.print(" ");
else if (count > 0 && count % yearsPerLine == 0)
System.out.println();
System.out.print(year);
++count;
}
}
}
}
int startFromYear = 2001;
int stopAtYear = 2100;
int count = 0;
for(int i = startFromYear; i <= stopAtYear; i++){
if((i % 4 == 0 && i % 100 != 0)||(i % 400 == 0)){
System.out.print(i + " ");
count++;
if(count % 10 ==0)
System.out.println();
}
}
100% correct! and easy to understand.
public class LeapYears
{
public static void main(String[] args)
{
int count = 0;
for(int i = 2001; i <= 2100; i++)
{
if ((i % 4 == 0 && i % 100 != 0)||(i % 400 == 0))
{
count++;
//if count is 10 then start a new line.
if(count % 10 == 0)
{
System.out.println(i);
}
//if count is smaller 10 then in the same line
else
{
System.out.print(i + " ");
}
}
}
}
}