Question

Help! in Java, test values {0, 1, 3, 6, 10} I need this to print 107...

Help! in Java, test values {0, 1, 3, 6, 10} I need this to print 107 but it keeps printing 75. What am I doing wrong???

    public int switchCase( int []values) {
       int sum = 0;
       for(int i = 0;i<values.length;i++){
           sum += values[i];
              }
          
           int value = values.length;
           switch (sum){
              case 1:
                  sum += value + 0;
                  break;
              case 2:
              case 3:
              case 4:
                  sum += value + 1;
                  break;
              case 5:
              case 6:
              case 7:
                  sum += value + value;
                  break;
           default:
               sum += value + value*10;
               break;
              }

               return sum;
       }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER: In your code as we know that the sum of the array elements is 20 and in the switch case you are sending sum as switch(sum) therefore case 20 should be there in the switch case but there is no case 20 so it is going in to the default statement where sum+= value+ value*10 and we know value =5 , so sum = 20 + 5+ 5*10 => 75 that's why it is printing the value 75.

And if you want to print 107 then you have to change the logic and you can change by two ways :1st you can add case 20 so that it can go into it and write logic for 107 as  I given below:

CODE:

public class Demo {
   public static int switchCase( int []values) {
   int sum = 0;
   for(int i = 0;i<values.length;i++){
   sum += values[i];
   }
  
   int value = values.length;
   switch (sum){
   case 1:
   sum += value + 0;
   break;
   case 2:
   case 3:
   case 4:
   sum += value + 1;
   break;
   case 5:
   case 6:
   case 7:
   sum += value + value;
   break;
   case 20:
       sum+= value+ 82;
       break;
   default:
   sum += value + value*10;
   break;
   }

   return sum;
   }
   public static void main (String[] args)
   {   
       int [] values = {0, 1, 3, 6, 10};
       System.out.println(switchCase(values));
   }
   }

OUTPUT:

107

or you can change the logic in to the default statement so that you can get 107.

Add a comment
Know the answer?
Add Answer to:
Help! in Java, test values {0, 1, 3, 6, 10} I need this to print 107...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA I need a switch so that users can input one selection then another. These selections...

    JAVA I need a switch so that users can input one selection then another. These selections are for a simple calculator that uses random numbers 1. + 2. - 3. * 4./ 5. RNG. This has to be simple this is a beginners class. Here is what I have import java.util.Scanner; import java.util.Random; import java.util.*; public class You_Michael02Basic_Calculator {    public static void main(String[] args) {        // Generate random numbers        int num1,num2;        num1 =(int)(Math.random()*100+1);...

  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • I had a few questions in Computer Science (Java). I got these questions wrong and I...

    I had a few questions in Computer Science (Java). I got these questions wrong and I would like the CORRECT answer and an EXPLANATION PLEASE. Please use the correct number when giving the answer such as 1a:. 1b: etc 1. For Each of the following Bits of code enter the final value of the variable names ans. a. int x = 23; int y = 7; Int ans = x % y; My answer 1.61 (Wrong It was 2) b....

  • Draw a picture of array A, showing the values stored in it after execution of each...

    Draw a picture of array A, showing the values stored in it after execution of each code segment below. Use the following data: 0 1 2 3 4 5 6 7 8 9 int A[4][2]: int k, m: for (int i = 0: i < = 3: i++) for (int j = 0: j < = 1: j++) A[i][j] = 0: for (int j = 1: j < = 10: j++) { cin > > k: switch (k) { case...

  • Hi everyone! I need help on my Java assignment. I need to create a method that...

    Hi everyone! I need help on my Java assignment. I need to create a method that is called sumIt that will sum two values and will return the value.It should also invoke cubeIt from the sum of the two values. I need to change the currecnt program that I have to make it input two values from the console. The method has to be called sumIt and will return to the sum that will produce the cube of the sum...

  • I am using Java to try and solve this problem. Suggestions on where I am going...

    I am using Java to try and solve this problem. Suggestions on where I am going wrong? PREVNEXT Workbench ? a Exercise 20662- WORK AREA RESULTS Write the definition of a method named sumArray that has one parameter, an array of ints. The method returns the sum of the elements of the array as an int. x9 of 9: 2018-07-10 12:58:15 -W SUBMIT 1 public int sumArray(int[] a) 2 int i; int sum = 0; for (í 0; í?a.length; ?++)...

  • Debug the following java code so that it will have given out put at the bottom....

    Debug the following java code so that it will have given out put at the bottom. Make sure to fix all syntax and logical errors. import java.util.Scanner; public class Graphs { // draws 5 histograms public void drawHistograms() Scanner input = new Scanner( System.in ); int number1 = 0; // first number int number2 = 0; // second number int number3 = 0; // third number int number4 = 0; // fourth number int number5 = 0; // fifth number...

  • Language is Java. Thank you.It is a review test and I will find out the rest...

    Language is Java. Thank you.It is a review test and I will find out the rest if I just know that answers. 1) What is the output of the following code: public class Test public static void main(String] args) [ String s1 new String("Java String s2 new String("Java) System.out.print( (s1 s2)(s1.equals (s2))) A) false false B) true true C) false true D) true false E) None of the above 2) Given the following program: public class Test f public static...

  • //please help I can’t figure out how to print and can’t get the random numbers to...

    //please help I can’t figure out how to print and can’t get the random numbers to print. Please help, I have the prompt attached. Thanks import java.util.*; public class Test { /** Main method */ public static void main(String[] args) { double[][] matrix = getMatrix(); // Display the sum of each column for (int col = 0; col < matrix[0].length; col++) { System.out.println( "Sum " + col + " is " + sumColumn(matrix, col)); } } /** getMatrix initializes an...

  • The loop below stores the values 1 to 5 in myList: for (int i = 0;...

    The loop below stores the values 1 to 5 in myList: for (int i = 0; i < 5; i ++) { myList [ i ] = i + 1; //store values using a loop } The loop below prints the values from first to last in myList: for (int i = 0; i < myList.length; i++) { System.out.print(myList [ i ] + " "); } Use the examples on the slides to complete the lab below. Please submit the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT