Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

Error in out

Im  trying to execute the value of P, what am I doing wrong?


class Examplechapter7ques9
{
public static void main(String[] args)
{

int x = 7;
int p=10;
switch (x)
{
case 1: p=15;break;
case 2: p=25;break;
case 3: p=30;break;
case 4: p=35;break;
case 5: p=40;break;
System.out.println(" "+p  );
}    
}
}
30-april.JPG
0
waltbaby315
Asked:
waltbaby315
1 Solution
 
mrjoltcolaCommented:
switch(x) means the case statement uses x for its value. Your cases don't include 7 (the value of x) so you need to put a "default" case, or add 7 as a case. Use a default anyway, for best practice.
0
 
waltbaby315Author Commented:
can you give me an example that?
0
 
cmalakarCommented:
after break, you cannot put any statements.
class Examplechapter7ques9 
{
public static void main(String[] args)
{

int x = 7;
int p=10;
switch (x)
{
case 1: p=15;break;
case 2: p=25;break;
case 3: p=30;break;
case 4: p=35;break;
case 5: p=40;break;
}
System.out.println(" "+p  );     
}
}

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
cmalakarCommented:
x values doesnot match with any case values (1, 2,3,4 or 5).

You can either write a case with matching value.. ie., 7 or write default.

as shown.
class Examplechapter7ques9 
{
public static void main(String[] args)
{

int x = 7;
int p=10;
switch (x)
{
case 1: p=15;break;
case 2: p=25;break;
case 3: p=30;break;
case 4: p=35;break;
case 5: p=40;break;
case 6: p=45;break;
case 7: p=50;break;  //matching 7 case
default: p=100;  //default.. will be executed if none of the case values matches with x.
}
System.out.println(" "+p  );     
}
}

Open in new window

0
 
printnix63Commented:
javac -cp . Examplechapter7ques9.java
javac -cp . Examplechapter7ques9.java
Process started >>>
<<< Process finished.
================ READY ================
java -cp . Examplechapter7ques9
java -cp . Examplechapter7ques9
Process started >>>
Wrong Value
 10
<<< Process finished.
================ READY ================

As said above, you will have to catch the case, where none of above will take.
class Examplechapter7ques9
{
	public static void main(String[] args)
	{

		int x = 7;
		int p=10;
		switch (x)
		{
			case 1: p=15;break;
			case 2: p=25;break;
			case 3: p=30;break;
			case 4: p=35;break;
			case 5: p=40;break;
			default: System.out.println("Wrong Value");
			System.out.println(" "+p  );
		}    
	}
}

Open in new window

0
 
printnix63Commented:
.. ok, cmalakar was quicker and even took care to place the System out outside the switch ...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now