Tuesday 15 March 2011

variables - Checking if a letter == a specified letter in Java -



variables - Checking if a letter == a specified letter in Java -

i trying perform check create sure letter typed user between , e. have tried several different ways, nil seems work. help appreciated. main problem is, checks once. after tells me need come in letter between , e, , set g, allows programme go on running afterwards.

system.out.println("please come in letter indicating taxation category: no taxation deduction, b 10% of gross pay, \nc 20% of gross pay, d 29% of gross pay, e 35% of gross pay"); //use ascii of letters compare int a_ascii = (int)'a'; int e_ascii = (int)'e'; int asciiletter = 0; // inquire user letter letter = in.next().charat(0); // convert letter uppercase , find ascii value if(character.isletter(letter)){ letter = character.touppercase(letter); asciiletter = (int) letter; while (asciiletter>=a_ascii&&asciiletter<=e_ascii) { if(asciiletter>=a_ascii&&asciiletter<=e_ascii){ if (asciiletter=='a'||asciiletter=='a'){ letter = letter; gross = gross; } else if (asciiletter=='b'||asciiletter=='b'){ letter = letter; deduct = (gross*0.1); taxation = gross - deduct; } else if (asciiletter=='c'||asciiletter=='c'){ letter = letter; deduct = (gross*0.2); taxation = gross - deduct; } else if (asciiletter=='d'||asciiletter=='d'){ letter = letter; deduct = (gross*0.29); taxation = gross - deduct; } else if (asciiletter=='e'||asciiletter=='e'){ letter = letter; deduct = (gross*0.35); taxation = gross - deduct; } } else { system.out.println("you must come in letter between , e."); letter = in.next().charat(0); } }

i don't know how doesn't work. however, there's problem in code handles invalid entries.

if want compare letter see if equals 1 of 2 or more characters, can say

if (letter == 'a' || letter == 'a')

however, if want create sure not equal any of those, must alter || && in add-on changing == !=:

if (letter != 'a' && letter != 'a')

the reason language take code literally. in english, might "if letter isn't or b or c". in program, if say

if (letter != 'a' || letter != 'a')

suppose letter 'a'. first operand, letter != 'a', false. sec operand, letter != 'a', true, because 'a' != 'a'. , ||, if either operand true, whole result true. in fact, if statement true regardless of letter is. isn't want. either utilize &&, or write this:

if (!(letter == 'a' || letter == 'a'))

by putting whole || in parentheses , negating !, you're guaranteed opposite result of

if (letter == 'a' || letter == 'a')

more: reply question in comments: reason you're in infinite loop this: inquire user letter. say, essentially,

while (letter [is 1 of valid ones]) { if (letter a) { ... } else if (letter b) { ... } ... other letters else ... seek user come in letter ------b } [past end of while] ------a

the way while works this: checks if status true. if it's false, jumps past end of while, point in above. if it's true, executes code in loop, , goes top, , checks status again.

so if user enters invalid letter, programme skips on whole loop, point a. never code tell user seek again. if letter valid, programme executes logic compute taxation , whatever--and goes top, , since letter hasn't changed, status still true, , goes through loop again, , again, ...

it looks wanted loop user come in letter if first letter invalid. since that's case, want while loop stops when letter valid, , keeps going when letter invalid. means want while status tests whether letter invalid, exit loop when it's valid--and then, after exit loop, want execute taxation logic, means computation has happen after end of while loop. like:

system.out.println("please come in letter indicating taxation category: no taxation deduction, b 10% of gross pay, \nc 20% of gross pay, d 29% of gross pay, e 35% of gross pay"); letter = in.next().charat(0); while (letter!='a'&&letter!='a'&&letter!='b'...) { // while letter invalid system.out.println("you must come in letter between , e."); letter = in.next().charat(0); } computation

i've tried reply in way shows why code wasn't working, , little changes create prepare it. please read other suggestions other ways can improve code.

java variables while-loop

No comments:

Post a Comment