Jump to content

no result of switch case

buklu

You're missing a break and it's also always recommended to have a default:

 switch (ch) {
            case 'a':  x = 1;
     		break;
            case 'b':  x = 2;
     		break;
            case 'c':  x = 3;
            	break;
     		//But what if 'ch' was not a, b  or c??
            default: x = 0;
        }
System.out.print("x now is: " + x);

 

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Minibois said:

You're missing a break and it's also always recommended to have a default:


 switch (ch) {
            case 'a':  x = 1;
     		break;
            case 'b':  x = 2;
     		break;
            case 'c':  x = 3;
            	break;
     		//But what if 'ch' was not a, b  or c??
            default: x = 0;
        }
System.out.print("x now is: " + x);

 

10.PNG.88541bbb5e1c85d7824e22000e932c0c.PNG

what is the function of 10+1?

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, buklu said:

10.PNG.88541bbb5e1c85d7824e22000e932c0c.PNG

what is the function of 10+1?

the Math.random method normally provides you with a double variable (a number value that can hold decimals, int variables hold numbers without decimals. This is important info for later in this explanation).

This method will give you at least 0.0, at most will give you 0.9 (repeating 9's). By doing *10, you make it so instead of getting 0.15, you get 01.5. Of course this is placed into an int, so you are losing the decimal points. So at this point you get the numbers (pseudo)randomly: 0, 1, 2, [...], 8 or 9.

You do +1 so that instead of getting at least 0, you get at least 1. You also do that +1 so you get at most 10.

 

So with this method Java gives you a (pseudo)random number from this list: 1, 2, 3, 4, 5, 6, 7, 8, 9 or 10.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

On 11/28/2018 at 8:57 PM, Minibois said:

the Math.random method normally provides you with a double variable (a number value that can hold decimals, int variables hold numbers without decimals. This is important info for later in this explanation).

This method will give you at least 0.0, at most will give you 0.9 (repeating 9's). By doing *10, you make it so instead of getting 0.15, you get 01.5. Of course this is placed into an int, so you are losing the decimal points. So at this point you get the numbers (pseudo)randomly: 0, 1, 2, [...], 8 or 9.

You do +1 so that instead of getting at least 0, you get at least 1. You also do that +1 so you get at most 10.

 

So with this method Java gives you a (pseudo)random number from this list: 1, 2, 3, 4, 5, 6, 7, 8, 9 or 10.

pro.PNG.d3f04868a3089485be7e3bfd7843caed.PNG

also,where should \n be placed on total surface area?thanks bro 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, buklu said:

 

also,where should \n be placed on total surface area?thanks bro 

I don't understand your question. Using \n inside a String will give you an enter (next line). Is that what you are after?

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, Minibois said:

I don't understand your question. Using \n inside a String will give you an enter (next line). Is that what you are after?

and also the error of %.2f,can you explain?

Link to comment
Share on other sites

Link to post
Share on other sites

7 hours ago, buklu said:

and also the error of %.2f,can you explain?

As far as I know you have to approach %.2f a little something like this:

"Your results is: " + String.format("%.2f", numberYouUse);

 

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×