0
import java.util.*;
import javax.swing.*;
public class ZhangIDK
{
    public static void main (String[] args)
    {
        average (1, 2, 3, 4, 5, 6, 7);
        least (1, 2, 3, 4, 5);
        Scanner sc = new Scanner (System.in);
        System.out.println ("Please enter a number");
        int h = sc.nextInt();
        System.out.println ("Please enter another number");
        int i = sc.nextInt();
        power (h, i);
        System.out.println ("Please enter a number");
        int k = sc.nextInt();
        System.out.println ("Please enter a word");
        String j = sc. nextLine();
        repeater (j, k);

    }

    public static void average (int a, int b, int c, int d, int e, int f, int g)
    {
        int y = a + b + c + d + e + f + g;
        y/=7;
        System.out.println ("The average is" +y);
    }

    public static void least (int q, int r, int s, int t, int u)
    {
        int Smallestnumber = 100;
        int number = 1;
        if (number < Smallestnumber)
        {
            Smallestnumber = number;
            number++;
        }
        System.out.println("The smallest number is" +Smallestnumber+ ".");
    }

    public static void power (int h, int i)
    {
        int result = 1;
        for (int temp = 0; temp < i; temp++)
        {
            result *= h;

        }
        System.out.println (h);

    }

    public static void repeater (String j, int k)
    {
        for (int tem = 0; tem < k; tem++)
        {
            System.out.println (j + " ");
        }

    }
}

I'm a beginner at programming and my teacher assigned us these method practice problems.

In the first method, we were suppose to find the average of 7 pre declared numbers.

In the second method, we were suppose to find the smallest of 5 pre declared numbers.

In the third method, we were suppose to allow user to input two numbers which the program will raise the first number to the power of the second. For example: the user inputs 2, and 4, the program will SOP 2^4, which is 16.

In the fourth method, we were suppose to allow the use to input a word and a number and the program should SOP the word as many times as the number. For example: the user inputs BOB and 2, the program should SOP BOB BOB

My problem is with the last method, the question will pop up asking "Please enter a word" but the program wont allow me to type a word and therefore I have no way of checking if my code works. How do I get the code to let me enter in a word?

A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
Sarah
  • 25
  • 7
  • for your power method, are you printing the wrong information? I think you should print result instead of h – Mc Kevin Mar 05 '14 at 02:06
  • THANK YOU!!!! I was such an idiot! I should have printed out result! THANKS SO MUCH! – Sarah Mar 05 '14 at 02:08

1 Answers1

0

You are doing your least method wrongly, the least method should have at least compared with each values you taken in as parameter. You can start by assigning the smallestnumber to your first value, let's say q and compare your smallestnumber with other values. If they are smaller than the smallestnumber, then you assign the value of the comparing value to the smallestnumber.

As for why your Please enter a word is not accepting input is that, you are calling sc.nextInt(), which is not wrong, but it will read the next Integer as the method name says, and ignore the rest of the line. So your sc.nextline() probably is accepting what's left on the scanner buffer, which is the newline character. To fix this, try inserting sc.nextline() after every sc.nextInt() or use sc.nextline() for every integer input and parse them to integer afterwards.

Mc Kevin
  • 962
  • 10
  • 31