determining logic error that causes a misprint - java

So, I am pretty new to java and I wanted to try my hand at debugging in command line, namely using jdb on a code that has a couple errors. I was tinkering with this code:
import java.util.Scanner;
import java.io.File;
class LetterHome{
static final int MAX_CODE = 5;
public static void main(String[] args) throws Exception{
Scanner in = new Scanner(new File(args[0]));
String phrase;
int sentenceCode, modifierCode;
System.out.println("Dear Mom and Dad:\n");
while( in.hasNext() ){
sentenceCode = in.nextInt();
modifierCode = in.nextInt();
if( (sentenceCode < 1) || (sentenceCode >= MAX_CODE) ) {
System.out.println(sentenceCode + " is not a valid sentence code");
continue;
}
if( sentenceCode == 1 ){
if( modifierCode == 1 ){
phrase = "great";
}else if( modifierCode == 2 ){
phrase = "ok";
}else{
phrase = "ERROR";
}
System.out.println("My classes are going " + phrase + ".");
}else if( sentenceCode == 2 ){
phrase = weatherModifier(modifierCode);
System.out.println("The weather here has been " + phrase + ".");
}else if( sentenceCode == 3 ){
if( modifierCode == 1 ){
phrase = "after the quarter ends";
}else if( modifierCode == 2 ){
phrase = "in a few weeks";
}else if( modifierCode == 3 ){
phrase = "next weekend";
}else{
phrase = "ERROR";
}
System.out.println("I plan to come home for a visit " + phrase + ".");
}else if( sentenceCode == 4 ){
System.out.println("Do you think you could send me $" + modifierCode + "?");
System.out.println("I have to buy another book for one of my classes.");
}else if( sentenceCode == 5 ){
if( modifierCode == 1 ){
phrase = "cookies";
}else if( modifierCode == 2 ){
phrase = "stuff";
}else; if( modifierCode == 3 ){
phrase = "money";
}else{
phrase = "ERROR";
}
System.out.println("Thanks for the " + phrase + " you sent.");
}
}
}
static String weatherModifier(int m) {
String word=null;
if(m == 1)
word = "great";
if(m == 2)
word = "foggy";
if(m == 3)
word = "hot";
if(m == 4)
word = "cold";
if(m == 5)
word = "variable";
if( m<1 && m>5)
word = "ERROR";
return word;
}
}
And I've already found a ";" that was out of place. I know that something is off with the values assigned to the terms in the end (I think), because when I compile the program, I get this output:
Dear Mom and Dad:
//
// 5 is not a valid sentence code
// My classes are going great.
// The weather here has been foggy.
// I plan to come home for a visit in a few weeks.
Instead of classes going "great", I get "foggy", which I noticed when I ran jdb. I ran the code with this data file:
5 2
1 1
2 1
3 2
The code itself prints a template with possible options outlined in the data file, which you may have already noticed. Here's the full list of possibilities:
// 1. My classes are going _____.
// 1. great
// 2. ok
// 2. The weather here has been _____.
// 1. great
// 2. foggy
// 3. hot
// 4. cold
// 5. variable
// 3. I plan to come home for a visit _____.
// 1. after the quarter ends
// 2. in a few weeks
// 3. next weekend
// 4. Do you think you could send me $_____?
// I have to buy another book for one of my classes.
// 5. Thanks for the _____ you sent.
// 1. cookies
// 2. stuff
// 3. money
However, I'm not sure what's off about the logic in the code. Any tips? Please let me know if I need to make some clarification. Thanks.

You added an extra ;at the end of if (m==2); in your weatherModifier function. Remove it!

I figured out the issue, actually:
It was in " if( (sentenceCode < 1) || (sentenceCode >= MAX_CODE) )"
I had to change ">=" to ">"

Related

Connect 4 game exception handler for array In Java

I already finished my program it is a connect 4 console game the only problem is I don't know where should I place my ArrayIndexOutOfBoundsException in the code in order for it to this is what I want to put in the code
try {
int element = Input.nextInt();
System.out.println("Element in the given index is :: "+myArray[element]);
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("The index you have entered is invalid");
System.out.println("Please enter an index number between 0 and 6");
}
this is the game code it is made out of 3 classes
I'll post the game Engine for the program if it is not helpful I'll post the gameboard
public class C4GameEngine {
public static void main(String[] args) throws Exception {
String Start = "+------------------------------------------------------------------------------+\n" +
"| Welcome to Connect Four console application. |\n" +
"| Each player alternately drops a 'R' or 'Y' into one of the seven columns. |\n" +
"| The objective of the game is to connect four of the same characters in |\n" +
"| a row, column or diagonal. |\n" +
"+------------------------------------------------------------------------------+ ";
System.out.println(Start);
//We will use the scanner for using input have Input as reference variable
Scanner Input = new Scanner(System.in);
//making the red player enter their name
System.out.println("Red player enter your name: ");
String red = Input.nextLine();
//calling out the player class
PlayerClass p1 = new PlayerClass(red, "R");
//yellow player to enter their name
System.out.println("Yellow player enter your name: ");
String yellow = Input.nextLine();
//calling the player class for the yellow player
PlayerClass p2 = new PlayerClass(yellow, "Y");
// creating the while loop the purpose of it is to make the player decide if they want to keep playing
while (true) {
System.out.print("Do you want to keep playing? Type Y or N: ");
if (Input.nextLine().equalsIgnoreCase("Y")) {
//making the board game pattern and calling the connect four board
String[][] board = C4Board.createPattern();
boolean loop = true;
//in order to keep track of the turns
int count = 0;
C4Board.drawPattern(board);
while (loop) {
//this will let the red have the first turn then after every other turn yellow will play
if (count % 2 == 0) C4Board.dropRedPattern(board, p1.getName()); //getting the name
else C4Board.dropYellowPattern(board, p2.getName());
count++; //keeping track of the turns
C4Board.drawPattern(board);
if (C4Board.checkWinner(board) != null) {
//using the if statment to print out the winner if it is red
//one point will be set to red and then the question will repeat
//do you want to keep playing if you say Y you'll go back to the
//new game until you go with N it'll get out of the loop and show you the stats
if (C4Board.checkWinner(board) == "R") {
System.out.println("Red won.");
p1.setGameWon();
} else if (C4Board.checkWinner(board) == "Y") {
System.out.println("Yellow won.");
p2.setGameWon();
}
loop = false;
}
}
} else if (Input.nextLine().equalsIgnoreCase("N")) {
System.out.println(p1.toString());
System.out.println("--------------------");
System.out.println(p2.toString());
System.exit(0);
}
And now the gameboard
import java.util.Scanner;
//Creating the connect four board body
public class C4Board {
//We need to first create the basic visual pattern first using 2D array
public static String[][] createPattern() {
//creating a double array named field that will hold the length of array
String[][] field = new String[7][15];
// creating a loop for each row
for (int i =0;i<field.length;i++) {
// creating a loop for each column
for (int j =0;j<field[i].length;j++) {
if (j% 2 == 0){
//the column will be either empty or have a | depending on the disk
field[i][j] ="|";
} else {
field[i][j] = " ";
}
//creating the base for the row
if (i==6) field[i][j]= "-";
}
}
return field;
public static void drawPattern(String[][] f) {
for (int i =0;i<f.length;i++) {
for (int j=0;j<f[i].length;j++) {
System.out.print(f[i][j]);
}
System.out.println();
}
}
public static void dropRedPattern(String[][] f, String Name) {
//the first move would be is to have the User to tell us
//where he wants to drop the disk which column
System.out.println("+----------------------------------+\n");
//the Name String will display the end name of the user in game stat
System.out.print(Name + " drop a red disk at column (0–6):\n ");
System.out.println("+----------------------------------+\n");
//using it for keyboard input
Scanner Input = new Scanner (System.in);
int c = 2*Input.nextInt()+1;
for (int i =5;i>=0;i--) {
if (f[i][c] == " ") {
f[i][c] = "R";
break;
}
}
}
//this is the same as for the yellow disk
public static void dropYellowPattern(String[][] f, String Name) {
System.out.println("+----------------------------------+\n");
System.out.print(Name + " drop a Yellow disk at column (0–6):\n ");
System.out.println("+----------------------------------+\n");
Scanner scan = new Scanner (System.in);
int c = 2*scan.nextInt()+1;
for (int i =5;i>=0;i--) {
if (f[i][c] == " ") {
f[i][c] = "Y";
break;
}
}
}
//for the check winner in connect four you can win in many ways
//so there is going to be a for loop checking each of those
public static String checkWinner(String[][] f) {
//checking horizontal line
for (int i =0;i<6;i++) {
//the red or yellow disk or nothing will be in odd places
//we'll start from 0 (which will be 1) and stop at 6 (which will be 7)
for (int j=0;j<7;j+=2) {
if ((f[i][j+1] != " ")
&& (f[i][j+3] != " ")
&& (f[i][j+5] != " ")
&& (f[i][j+7] != " ")
&& ((f[i][j+1] == f[i][j+3])
&& (f[i][j+3] == f[i][j+5])
&& (f[i][j+5] == f[i][j+7])))
//returning the color if the pattern is the same
return f[i][j+1];
}
}
//vertical line looping over each odd column to check for repeated boxes in the same column
for (int i=1;i<15;i+=2) {
//we are vertically checking each line of the code now by the for loop statement so it's from 0 to 2
for (int j =0;j<3;j++) {
if((f[j][i] != " ")
&& (f[j+1][i] != " ")
&& (f[j+2][i] != " ")
&& (f[j+3][i] != " ")
&& ((f[j][i] == f[j+1][i])
&& (f[j+1][i] == f[j+2][i])
&& (f[j+2][i] == f[j+3][i])))
return f[j][i];
}
}
//For the diagonal line the loop has to go over the 3 uppermost rows
//then left to right column
for (int i=0;i<3;i++) {
for (int j=1;j<9;j+=2) {
//still using the odd index for the column and incrementing by 2
if((f[i][j] != " ")
&& (f[i+1][j+2] != " ")
&& (f[i+2][j+4] != " ")
&& (f[i+3][j+6] != " ")
&& ((f[i][j] == f[i+1][j+2])
&& (f[i+1][j+2] == f[i+2][j+4])
&& (f[i+2][j+4] == f[i+3][j+6])))
return f[i][j];
}
}
//this is the same as above but instead of going left to right we will go the opposing direction
for (int i=0;i<3;i++) {
for (int j=7;j<15;j+=2) {
if((f[i][j] != " ")
&& (f[i+1][j-2] != " ")
&& (f[i+2][j-4] != " ")
&& (f[i+3][j-6] != " ")
&& ((f[i][j] == f[i+1][j-2])
&& (f[i+1][j-2] == f[i+2][j-4])
&& (f[i+2][j-4] == f[i+3][j-6])))
return f[i][j];
}
}
//the null statement will return nothing if it doesn't find any disk anywhere
return null;
}
}

Need to run Java code in GWT

I created a new google web application project (using eclipse) and noticed the code runs in the .server package. I have a slot machine game i wrote in java and am trying to implement it into GWT, but the syntax seems to be totally different. (for example, i noticed \n doesn't work)
Here is the code i want to implement - how the heck would i do that?
// clear console
static void clearConsole()
{
for (int i=0; i<25; i++)
{
System.out.println("\n");
}
}
// actual slot machine algorithm
static void slots()
{
Scanner input = new Scanner (System.in);
char newGame = ' ';
int chips = 100;
int bet = 0;
do{
System.out.println( "Try your luck on the SLOT MACHINE." +
"\nLine up (=^^=) or ))<>(( symbols to win big!\n\n" +
"You currently have " + chips + " chips.\nHow much do you want to bet? ");
//check for accidental char input
try{
bet = input.nextInt();
}
catch(Exception e){
input.nextLine();
System.out.println("NOT A VALID NUMBER\nHow much do you want to bet? ");
bet = input.nextInt();
}
if (bet<=chips && bet>0){
// to add some realism, slot machine will not execute until 'enter' pressed
// then console cleared
input.nextLine();
System.out.println( "\nPress 'enter' to start the slot machine.");
input.nextLine();
clearConsole();
String[] machine = {"(=^^=)", "(=^^=)", "))<>((", " XXXX ", " XXXX ", " :^{) ",
" :^{) ", " (>_<)", " (>_<)", " [-O-]", " [-O-]"};
Random rand = new Random();
int slot1 = rand.nextInt(11);
int slot2 = rand.nextInt(11);
int slot3 = rand.nextInt(11);
int slot4 = rand.nextInt(11);
int slot5 = rand.nextInt(11);
int slot6 = rand.nextInt(11);
int slot7 = rand.nextInt(11);
int slot8 = rand.nextInt(11);
int slot9 = rand.nextInt(11);
System.out.println( "-----------------------");
System.out.printf( "%-7s %-7s %-7s %n%n", machine[slot1], machine[slot2], machine[slot3]);
System.out.printf( "%-7s %-7s %-7s %n%n", machine[slot4], machine[slot5], machine[slot6]);
System.out.printf( "%-7s %-7s %-7s %n", machine[slot7], machine[slot8], machine[slot9]);
System.out.println( "-----------------------\n\n\n\n");
// 3 wild cards
if (slot4 == 2 && slot5 == 2 && slot6 == 2 ){
bet = bet*100;
chips = chips + bet;
System.out.println( "JACKPOT! ");
}
//3 cats (inclusive of wild card)
else if ( slot4 <3 && slot5 <3 && slot6 <3 ){
bet = bet*5;
chips = chips + bet;
System.out.println( "YOU WIN! ");
}
// 3 of any other symbol (inclusive of wild card)
else if( ((slot4==2 || slot4==3 || slot4==4) && (slot5==2 || slot5==3 ||
slot5==4) && (slot6==2 || slot6==3 || slot6==4))
|| ((slot4==2 || slot4==5 || slot4==6) && (slot5==2 || slot5==5 ||
slot5==6) && (slot6==2 || slot6==5 || slot6==6))
|| ((slot4==2 || slot4==7 || slot4==8) && (slot5==2 || slot5==7 ||
slot5==8) && (slot6==2 || slot6==7 || slot6==8))
|| ((slot4==2 || slot4==9 || slot4==10) && (slot5==2 || slot5==9 ||
slot5==10) && (slot6==2 || slot6==9 || slot6==10)) ){
bet = bet*3;
chips = chips + bet;
System.out.println( "YOU WIN! ");
}
// 2 cats
else if ( slot4<2 && slot5<2 || slot4<2 && slot6<2 || slot5<2 && slot6<2){
bet = bet*2;
chips = chips + bet;
System.out.println( "YOU WIN! ");
}
// 1 cat
else if ( slot4 < 2 || slot5 < 2 || slot6 < 2 ){
chips = chips + bet;
System.out.println( "YOU WIN! ");
}
// nothing
else{
chips = chips - bet;
System.out.print( "You Lose... ");
}
// display current amount of chips
System.out.println( "You have " + chips + " chips.");
}else{
System.out.println( "You do not have enough chips to make that bet!");
}
if (chips > 0){
System.out.println( "\nDo you wanna play this game again? (y/n): ");
newGame = input.next().charAt(0);
clearConsole();
}
} while (newGame =='y' && chips > 0 );
input.close();
System.out.println( "\nGame Over\n\n");
}
You could replace System.out.println with putting raw text (not even HTML) into an element with fixed space etc (something like the "pre" element).
I'd suggest you switch to something like Button for "enter", "y"/"n"... and and DoubleBox for the amount they want to bet.
There may be a more general "console" in browser approach but it would be a bit odd?
Maybe best starting with one of the GWT samples and slowly converting to what you want? Quite a bit of bootstrapping to get a GWT app up and running.

While loop JOptionPane issues

Ok - I know that I am fairly close to the solution but need a nudge in the right direction. I need the user to hit YES and the program to begin asking the question again.
After execution, I get the following errors
Exception in thread "main" java.lang.StringIndexOutOfBoundsException:
String index out of range: 1 at java.lang.String.charAt(Unknown
Source) at Vowel3.main(Vowel3.java:49)
// java class for Panel I/O
import javax.swing.JOptionPane;
// declaration of the class
public class Vowel33
{
// declaration of main program
public static void main(String[] args)
{
// objects used to store data
String input_string = null;
int a_count = 0;
int e_count = 0;
int i_count = 0;
int o_count = 0;
int u_count = 0;
int i = 0;
int yes = 0;
// 1. display a descriptive message
String display_message = "This program asks the user for a sentence,\n"
+ "searches the sentence for all vowels,\n"
+ "and displays the number of times each"
+ "vowel appears in the sentence";
JOptionPane.showMessageDialog(null, display_message, "Lab 3 Description", JOptionPane.INFORMATION_MESSAGE);
// 4. visit each String posotion
do{
// 3. input the character string
input_string = JOptionPane.showInputDialog("Enter the sentence to search");
// 5. if position i of String is a vowel
// 6. increase the appropriate vowel counter
if (input_string.charAt(i) == 'a' || input_string.charAt(i) == 'A')
a_count++;
else if (input_string.charAt(i) == 'e' || input_string.charAt(i) == 'E')
e_count++;
else if (input_string.charAt(i) == 'i' || input_string.charAt(i) == 'I')
i_count++;
else if (input_string.charAt(i) == 'o' || input_string.charAt(i) == 'O')
o_count++;
else if (input_string.charAt(i) == 'u' || input_string.charAt(i) == 'U')
u_count++;
i++;
String display_message1 = input_string // 7. display the String
+ "\n\n" + "has " + input_string.length() + " characters.\n\n" // 8. display the number of characters
+ "There are \n"
+ a_count + " a's,\n" // 9. disaply the number of each vowel
+ e_count + " e's,\n"
+ i_count + " i's,\n"
+ o_count + " o's, and\n"
+ u_count + " u's.\n\n";
JOptionPane.showMessageDialog(null, display_message1, "Lab 3 Description", JOptionPane.INFORMATION_MESSAGE);
yes = JOptionPane.showConfirmDialog(null, "Would you like to enter another string?\n\n", "Extra Credit", JOptionPane.YES_NO_OPTION);
} while (i < input_string.length());
if (i == input_string.length())
{
yes = JOptionPane.showConfirmDialog(null, "Would you like to enter another string?\n\n", "Extra Credit", JOptionPane.YES_NO_OPTION);
if (yes == 1)
{
input_string = JOptionPane.showInputDialog("Enter the sentence to search");
}
}
} // end of main
} // end of the class
In your code you have a condition where the loop can still be executed if yes is 0. Since yes is never redefined from what I can see, you basically have an infinite loop and your code will error out when i exits the bounds of your string's length.
Also not sure why you have two JOptionPanes at the bottom of the loop (which would execute x times where x = input_string.length())
Consider something like:
if(i == input_string.length()) {
//ask the user if they want to enter another string
if(the user selected yes){
yes = 1;
//instantiate again with new input_string
}
}
Another note: Assuming you want to show a message asking if the user wants to enter another string once you've finished iterating through their first entry, it seems moot to have the yes variable and condition.

error :not a statement

So i started in September my university and i have a class that is introduction to programming and i have to do a program that recognize 3 numbers and tell how many are equal for example: 30 30 2 "2 numbers are equal" but i get error :not a statement on "else ( n1!=n2 && n2!=n3 && n3!=n1 ); {
import java.util.Scanner;
public class Equal {
public static void main(String[] args) {
Scanner in = new Scanner( System.in );
int n1, n2, n3;
int a = 3;
int b = 2;
int c = 0;
System.out.println("choose tree numbers:");
n1 = in.nextInt();
n2 = in.nextInt();
n3 = in.nextInt();
if ((n1==n2 && n1==n3 || n2==n1 && n2==n3 || n3==n1 && n3==n2)) {
//Then the tree numbers are equal;
System.out.println( "There are: " + a + " equal numbers" );
}
if ((n1!=n3 && n1==n2 || n2!=n1 && n2==n3 || n3!=n2 && n3==n1 )) {
//Then only two numbers ill be the same;
System.out.println( "There are: " + b + " equal numbers" );
}
else ((n1!=n2 && n2!=n3 && n3!=n1)); {
//All the numbers are not equal;
System.out.println( "There are: " + c + " equal numbers" );
}
}
}
change
else ( n1!=n2 && n2!=n3 && n3!=n1 ); {
to
else if ( n1!=n2 && n2!=n3 && n3!=n1 ) {
if (condition1) { do_something; }
else if (condition2) { do_something_else; }
else { do_a_different_thing; }
A bare else-clause, by definition, does not take a condition - it fires if the preceding if/else-if clauses don't fire. I guess you could think of it as an implicit condition: if (!condition && !condition2).
(But note that, in general, you are not required to follow the if/else-if/else pattern. You could have multiple ifs in a row with no else-ifs or else-clause, or you could have an if/else with no else-ifs, etc. It all depends on the exact logic of the situation.)
A couple of suggestions.
It's really good that you're thinking about all of the possible cases. But here, you have three numbers, and if you've already determined that it is NOT the case that all three of them match, and it is NOT the case that two of them match, then the only possibility left is that none of them do. So you don't have to test for that case explicitly.
Similarly, in your first condition, checking for a three-way match, you don't have to explicitly test every combination either. If n1==n2 && n1==n3, then by transitivity n2==n3 and all three match. The order doesn't matter.
Have fun!

Having an issueif else and if else statements.

I'm really new to java (third week of class), but I've been trying to work on this code for hours and I just can't seem to find an answer to what I'm doing. javac tells me I only have three errors, but I'm wondering if there's more than that.
Here's my code, and I know my average section still needs work but i just cant figure out what's going on with the middle section of if and else statements. Sorry if this is really dumb, and im sure my syntax is all over the place:
import java.util.Scanner;
public class Program1
{
static public void main( String args [ ] )
{
int grade;
int A,B,C,D,F;
A = 0;
B = 0;
C = 0;
D = 0;
F = 0;
System.out.println( "*************** Grade Computer *************");
// ********************** //
Scanner kbd = new Scanner (System.in);
System.out.println("Enter Students First Name: ");
String fname = kbd.next( );
System.out.println("Enter Students Middle Initial: ");
String mi = kbd.next( );
System.out.println("Enter Students Last Name: ");
String lname = kbd.next( );
System.out.println("Enter First Exam Grade: ");
int firstexam = kbd.nextInt( );
System.out.println("Enter Second Exam Grade: ");
int secondexam = kbd.nextInt( );
System.out.println("Enter Third Exam Grade: ");
int thirdexam = kbd.nextInt( );
System.out.println("Was the bonus done? [yes/no] : ");
boolean b = kbd.nextBoolean( );
boolean yes = true;
boolean no = false;
// *********************** //
if(true)
{
{
if((firstexam >= (secondexam * 0.60 ) ) & (firstexam >= (thirdexam * 0.80 )));
{
System.out.println(firstexam);
}
else if((secondexam * 0.60) >= (thirdexam * 0.80));
{
System.out.println(secondexam * 0.60);
}
else {
System.out.println(thirdexam * 0.80);
}
}
if(true)
{
if((secondexam >= firstexam) & ((thirdexam * 0.80) >= secondexam));
{
if(secondexam >= (thirdexam * 0.80));
{
System.out.println(secondexam);
}
}
else {
System.out.println(thirdexam * 0.80);
}
}
else {
System.out.println(firstexam);
System.out.println(secondexam);
System.out.println(thirdexam);
}
}
// ********************** //
System.out.println(" **********Grade Summary********** ");
double average = calcAverage(firstexam, secondexam, thirdexam);
System.out.println("Grade Report For: " + fname);
if (true)
{
System.out.println("Bonus was done so grades are adjusted if appropriate.");
}
else
{
System.out.println("Bonus was not done.");
}
System.out.println("Exam 1: " + firstexam);
System.out.println("Exam 2: " + secondexam);
System.out.println("Exam 3: " + thirdexam);
System.out.println("The average is: " + average);
determineGrade(average);
}
public static double calcAverage(int firstexam, int secondexam, int thirdexam)
{
double average = (firstexam + secondexam + thirdexam) / 3.0;
return average;
}
public static void determineGrade(double average)
{
if (average>90)
{
System.out.println("Grade: A");
}
else if (average>=80)
{
System.out.println("Grade: B");
}
else if (average>=70)
{
System.out.println("Grade: C");
}
else if (average>=60)
{
System.out.println("Grade: D");
}
else if (average<60)
{
System.out.println("Grade: F");
}
}
// ************** //
}
Your if statements having ; in the end
if((firstexam >= (secondexam * 0.60 ) ) & (firstexam >= (thirdexam * 0.80 )));
They are considering as statements and proceeding further.
Remove all of them in the end of each statement.
if((firstexam >= (secondexam * 0.60 ) ) & (firstexam >= (thirdexam * 0.80 ))) (;)
The ; shouldn't be here.
Difference between & and && :
& <-- verifies both operands
&& <-- stops evaluating if the first operand evaluates to false since the result will be false
(x != 0) & (1/x > 1) <-- this means evaluate (x != 0) then evaluate (1/x > 1) then do the &. the problem is that for x=0 this will throw an exception.
(x != 0) && (1/x > 1) <-- this means evaluate (x != 0) and only if this is true then evaluate (1/x > 1) so if you have x=0 then this is perfectly safe and won't throw any exception if (x != 0) evaluates to false the whole thing directly evaluates to false without evaluating the (1/x > 1).
An other thing :
if(true)
{
{
This should be deleted because it just adds more code , it will be executed every time so no need to add it.
Besides the colon the end of the if statement you also should keep in mind that if you use
if(true){
}else{
}
The else statement will never execute cos the if will always be true, so you should be using the yes/no variables as flags for your if statement instead of the "true" itself.
If your statements inside the if should always be executed then you don't need the conditions at all.

Categories