3 Replies - 347 Views - Last Post: 09 October 2012 - 10:18 AM Rate Topic: -----

#1 Brovahkiin501  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 01-October 12

Java program to display inputs as many times as user wants

Posted 08 October 2012 - 11:29 PM

HI everyone!

I have been having a lot of trouble with this code /: We have to create a code with 3 overloaded methods, display(), to display something N times. We have to call the method display() for displaying. In the main method, we have to have the user input an integer, a double number, a string, and the number of times they want each thing displayed. This is the expected output:

Please input an integer: 22
Please input a rational number: 3.6
Please input a String: Hello
Please input the number of display times: 3
22
22
22
3.6
3.6
3.6
Hello
Hello
Hello

Here is the code I have so far...I know it's nor correct but if somebody could help me out and tell me what I need to do, it would be very much appreciated!

import java.util.Scanner;
public class DisplayMultiple {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.print("Please input an integer: ");
		int num = input.nextInt();
		System.out.print("Please input a rational number: ");
		double b = input.nextDouble();
		System.out.print("Please input a string: ");
		String str = input.next();
		System.out.print("Please input the number of display times: ");
		int N = input.nextInt();
		System.out.print(display(num, N));
		System.out.print(display(b, N));
		System.out.print(display(str, N));
	
	}

	public static void display(int num, int N) {
		System.out.print(num);
		
	}

	public static void display(double b, int N) {
		System.out.print(B)/>;

	}

	public static void display(String str, int N) {
		System.out.print(str);
		
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Java program to display inputs as many times as user wants

#2 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Java program to display inputs as many times as user wants

Posted 08 October 2012 - 11:43 PM

What I notice in your output is that you need to display the values that you have inputted a certain number of times.So, the best way you could go about this is by using a loop, preferably a while loop instead of a for loop, and use the value which you get in N to be the final value of the while loop.In that loop, you could put your print statements.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#3 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Java program to display inputs as many times as user wants

Posted 08 October 2012 - 11:56 PM

Also, in your overloaded functions, you could remove the use of the int N as you are not going to be needing that value there.

regards,
Raghav

This post has been edited by raghav.naganathan: 08 October 2012 - 11:56 PM

Was This Post Helpful? 0
  • +
  • -

#4 kellzor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-May 12

Re: Java program to display inputs as many times as user wants

Posted 09 October 2012 - 10:18 AM

your functions are all void so it doesn't return a value. either you can use the function to print the input for you, or make it a return type and print it out in the loop.

so returning an int assuming you want to do it that way is like..note the return type 'Integer' you will have to change this for the various inputs

	public static Integer display(int num) {
		return num;	
	}


then put N inside a simple for loop or while loop and print the return value

		int N = input.nextInt();
                
                for(int x=0; x<N;x++){
                    System.out.println(display(num));
        }

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1