1 Attachment(s)
simple multiplication exercise.
Id be glad if someone can explain whats wrong with my code below to achieve the goal of this exercise:
Write a times table programme. The programme should ask a user to input a number. This number is then used as the times table. So if the user enters 10, the 10 times table should be displayed. Your Output window should look something like this, when your programme is run.
Attachment 1386
my noob code ;
Code java:
package calculator;
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner omar = new Scanner(System.in);
System.out.println("Enter a number: ");
int fnum= 9;
int snum= 10;
for (int n=1; n<=snum; n++)
System.out.println(fnum + "x" + n + "=" + fnum*n);
Re: simple multiplication exercise.
Can you first tell us what is wrong? Are you getting a compilation error? Is the code throwing a run-time exception? Is it compiling and running but not behaving well? These details can help us help you.
Re: simple multiplication exercise.
In the future please use code tags. They make your code much more readable.
The first problem I can see is that the code is incomplete. You're missing at least a few closing braces at the end. This could be a copy-paste error, or you just need to add them. Properly formatting your code is the easiest way to catch these types of problems.
You say there's something wrong with your code. What is it doing that's wrong? What output are you getting? If there's a compile error or exception, what exception are you getting? Please post the full message/output.
Re: simple multiplication exercise.
Well im not getting the output I want.
run:
Enter a number:
9x1=9
9x2=18
9x3=27
9x4=36
9x5=45
9x6=54
9x7=63
9x8=72
9x9=81
9x10=90
BUILD SUCCESSFUL (total time: 0 seconds)
i want it so that i get the same output as the exercise results i posted above. wont let me input number ^^
i already defined int fnum= 9; but thats supposed to be the user input. how do i make it whatever the user inputs? ty <3
Re: simple multiplication exercise.
Quote:
how do i make it whatever the user inputs?
Use a method of the Scanner class to read in the number the user enters.
Re: simple multiplication exercise.
i tried like this but i get red underline error
int fnum= new Scanner(System.in);
int snum= 10;
Re: simple multiplication exercise.
Using a method of a class is a two step process.
First define an instance of the class
Then call one of its methods.
Code :
int fnum= new Scanner(System.in);
The new statement returns an instance of the class, not an int value
The code in post #1 was correct:
Code :
Scanner omar = new Scanner(System.in);
Now use the omar reference to a Scanner class object to call one of the Scanner class methods that reads from the user and returns an int value.
Re: simple multiplication exercise.
You need to assign the Scanner object to a Scanner variable. You can't assign it to an int.
Code java:
int fnum = new Scanner(System.in); // error: fnum is an int variable, not a Scanner
Scanner reader = new Scanner(System.in); // correct: reader is a scanner variable and can hold Scanner objects
You'll also need to specifically poll the scanner for an integer. Likely you'll want to use the nextInt() method. Read the Javadoc to determine how this method works and how to use it.