The code segment below is a part of a bigger program, but I think this is where I'm having the issue. So, I am trying to use a while loop to ask for input over and over until the program is done, and the dialog box is not coming up and I don't know why. I would really appreciate it if someone could take a look and tell me what I'm doing wrong. If you want to see the entire program just tell me.
Code
boolean done = true;
while(!done)
{
Grid grid = new Grid();
boolean userDone = true;
int compRandom = (int) (Math.random() * 9) + 1;
String input1 = JOptionPane.showInputDialog("Please input a number 1-9 for where you would like to place your piece: ");
Integer input = Integer.parseInt(input1);
boolean done = true;
must be
boolean done = false;
Related
Hi i'm trying to program a tool that allow us to manage a small shop , i've managed to get evey functionality to work`but this last function is getting me crazy
public void ajoutCommandesCli() {
Commandes com = new Commandes();
Magasin m = this;
Client c = new Client();
Scanner sc = new Scanner(System.in);
System.out.println("quel est votre numero client ?");
int numcli = sc.nextInt();
System.out.println("testosterone");
System.out.println(this.listecli.capacity());
for (int i = 0; i <= this.listecli.size(); i++) {
if (numcli == listecli.get(i).num) {
int index = this.listecli.indexOf(i);
c = this.listecli.get(index);
System.out.println("test aaaa");
com.saisirCommandes(this, c);
} else {
System.out.println("réessayer?");
System.out.println("1-oui");
System.out.println("2-non");
int choix = sc.nextInt();
if (choix == 1) {
this.ajoutCommandesCli();
} else {
System.out.println("testitestos");
MenuMagasin.afficherMenu(this);
}
}
}
}
this function doesnt even enter the first for loop and i dont know why .
Magasin has a vector of client ,but java tells me my vector capacity is 10 but the teacher told us vector just has no definite capacity , i havent inputed any lenghth for the vector.this is getting me crazy please help me guys .I'm sorry if my question sounds stupid but i need help.
Your question lacks information, you should make a minimal-reproducible-example as shown here.
I'd recommend running your program in debug mode, and view the value of this expression :
numcli==listecli.get(i).num
does your program ever run the if statement's code ?
is you list listecli empty ?
what's the error message you getting ?
Alright, so I'm trying to make a very rudimentary version of battleship in order to test out my one week's worth of programming knowledge. Basically, all the program does is have the user enter two numbers and those numbers are then used to locate a slot on a multidimensional boolean array. If the array's slot is true, it should say hit. If it is false, it should say miss. However, for some reason, no matter what I enter, I keep getting "Hit! Way to go!" Fortunately, the program is displaying an error for the "else" command. It says, "The following branch is never used." Unfortunately, I don't know why the branch is never used and I don't know how to fix it. Can I have some pointers, help, and/or advice? Thanks!
import java.util.Scanner;
class Battleship {
public static void main(String[] arguments) {
System.out.println(" Welcome to Battleship Solo Edition!");
System.out.println(" There are seven boats scattered across this 10 by 10 grid.");
System.out.println(" When you get a \'Hit!\', mark it on your accompanying paper.");
System.out.println(" Once you have completely shot down all seven boats (18 Hits), you win!");
System.out.println(" But play carefully! You only have 50 missiles.");
System.out.println();
boolean[][] field = new boolean [10][10];
//Ship one
field[8][2] = true;
field[8][3] = true;
field[8][4] = true;
field[8][5] = true;
field[8][6] = true;
//Ship two
field[1][9] = true;
field[2][9] = true;
//Ship three
field[2][4] = true;
//Ship four
field[2][2] = true;
field[3][2] = true;
field[4][2] = true;
//Ship five
field[4][8] = true;
field[5][8] = true;
field[6][8] = true;
field[7][8] = true;
//Ship six
field[6][3] = true;
field[6][4] = true;
//Ship seven
field[6][6] = true;
for (int missileattempts=0; missileattempts<=50; missileattempts++){
System.out.println("What is the X-Coordinate of your guess?");
Scanner scan = new Scanner(System.in);
int AnswerX = scan.nextInt();
System.out.println("What is the Y-Coordinate of your guess?");
Scanner scan2 = new Scanner(System.in);
int AnswerY = scan.nextInt();
if (field[AnswerX][AnswerY]=true){
System.out.println("Hit! Way to go!");}
else {System.out.println("Miss! Try again!");}
}
}}
a = b // assignment
a == b // comparison
You need to use the correct operator in your if condition.
Assignment results in the value from the right-hand side of the operator. Because you are assigning the value true to field[x][y], your condition will always evaluate to true.
In the if condition, you are assigning the value true to the variable, and then that gets checked. (It's always true at that point because that's what you assigned.
Two things here: First, the Boolean test operator is == rather than =. Second, if the variable is Boolean anyway, then you should just check it rather than doing an equality check at all, like this: if (field[AnswerX][AnswerY]) {. Doing equality checks on Booleans is bad style - in part because it lends itself to exactly the error that you have here.
So I am a student. I am taking my first class in Java. I am sure my code is horrible, but please don't beat me up, I am really trying to learn. I have read quite a few method questions and adjusted my code according the the suggestions on this website. The closest post to my problem is Running Loop on Java Method. I adjusted my code according to the suggestions in this post, along with a few others. That said, it did not fix my problem. My methods are not pulling through properly.
Can anyone help? It will not pull any of the Boolean code into the main and I am not sure if the Boolean is pulling my methods correctly. Intro runs properly. Below is my code (edited down to include main and bool code only):
public static void main(String[] args)
{
intro(); // This works
while (repeat()) //This does not work
;
}//end Main
//None of this is being called
public static boolean repeat()
{
//Assign Variables
char calculate, calculateCS, repeat;
double inchCircle, inchSphere, radiusCS, volume, volumeSphere, area, areaCircle;
String shape;
//Initialze Variables
calculate = 1;
area = 1;
volume = 1;
inchCircle = 1;
inchSphere = 1;
shape = "";
//Ask if user wants to repeat
System.out.print("Do you want to calculate anouther round object (Y/N): ");
if (!takeInput().equalsIgnoreCase("y"))
{
System.out.println("Thank you for using the Round Object Calculator. Goodbye. ");
return false;
}
else
{
printCircleSphere(shape);
if (!printCircleSphere(shape).equalsIgnoreCase("c"))
{
printRadiusC(inchCircle);
printCircle(area);
}
else if (!printCircleSphere(shape).equalsIgnoreCase("s"))
printRadiusS(inchSphere);
printSphere(volume);
}
return true;
}//end repeat
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("How many of your two bosses are smiling?");
int smiles = in.nextInt();
final boolean trouble;
if(smiles == 0 || smiles == 2){
trouble = true;
}else if(smiles == 1){
trouble = false;
if(trouble == false){
System.out.println("The coast is clear!");
}
else if(trouble == true){
System.out.println("You're in trouble!");
}in.close();
}
This whole thing is stupid.
I need to basically make a boolean-using program that tells the user, via the number of bosses smiling, wether or not they are in trouble.
Both/Neither smiling = trouble
One smiling and the other not smiling = no trouble
But it will only print the given strings if there's no trouble.
I get that I'm a fukken idiot, and this whole task I'm required to do is, personally, kinda stupid. But whatever, moron can't do simple code. Maybe someone could tell me what stupid mistake I made so I can get this done and out of the way?
It's resolved, I'm an idiot who can't read the bracket placement/formatting properly. Also yes, it shouldn't have been a final, though changing that was not the resolution at first.
Change
final boolean trouble;
to
boolean trouble = false;
Your boolean value is final and thus cannot be modified, its default value is "false". That means that it is never changed from "false" no matter what happens next.
By the way, if a number larger than 2 is input, the boolean value will also be "false".
A check to see if the integer being input is in the right range (between 0 and 2) would be a good idea.
I am making a basic calculator for Android in Java. I am stuck in one final step where it must add the input with the previous input. Have a look at the code:
public void displayValue(){
String mything = display.getText().toString();
input = Integer.parseInt(mything);
}
public void number1(View view){
if (input == 0){display.setText("");}
display.append(Integer.toString(1));
displayValue();
}
public void number2(View view){
if (input == 0){display.setText("");}
display.append(Integer.toString(2));
displayValue();
}
public void plus(View view){
displayValue(); //result= 0
result = result + input; //result= input
input = 0; //input=0
//in this step input becomes 0 to let the user enter new number input but this
//input never add the old result and the equal shows the old result.
}
public void equal(View view){
displayValue();
display.setText(Integer.toString(result));
}
I noticed that if I add a line in equal method and add the result to input I get the correct answer but that's not gonna be helpful as there will be minus button too.
Any ideas?
Thanks.
It's definitely hard to tell because you don't include full code, which would be helpful, but could it be because you call displayValue(); in some places before doing the math? Specifically in the plus method.
Ok finally I came up with a solution.
As the only option for calculation of result and new input is in equal method (because logically when user press the equal button, so wants to ends the equation), so I added two boolean values for each minus and plus calculation. Then I added both calculation for adding or minus 2 values.
Then when the user inter first part of the calculation and then hit plus sign, the boolean value of plus becomes true and after entering new input and hitting the equal sign, it does the true part of the calculation. Well it is a bit hard to explain but i guess by looking at the code you would get what I mean.
boolean whenMinus = false;
boolean whenPlus = false;
public void plus(View view){
displayValue();
result = input;
input = 0;
whenPlus = true;
}
public void minus(View view){
displayValue();
result = input;
input = 0;
whenMinus = true;
}
public void equal(View view){
if (whenPlus == true){result = result + input; whenPlus = false;}
if (whenMinus == true){result = result - input; whenMinus = false;}
display.setText(Integer.toString(result));
}
I am not sure if it is the correct way of making this calculator. But I fixed my problem anyway. It would be great to comment me and let me know if it is the standard way or its kinda hacking. I am not a pro anyway.