Doing multiples of a number in java issues with implementation [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Im not sure how to go about making this code.. well at least the total part of this. this is my code right now, I understand the math behind it. I'm just not sure how to implement it. Would I use a loop? here is my code as of right now. I know its not correct but its the start of it.
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("what number would you like me to multiply: ");
int number = in.nextInt();
System.out.println("how many multiples of "+number+" would you like to see: ");
int multiple = in.nextInt();
int total =
System.out.println("total :"+total);
}
here is the output that I would like to get:
What number would you like me to multiply? 4
How many multiples of 4 would you like to see? 7
The first 7 multiples of 4 are: 4, 8, 12, 16, 20, 24, 28

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("what number would you like me to multiply: ");
int number = in.nextInt();
System.out.println("how many multiples of "+number+" would you like to see: ");
int multiple = in.nextInt();
System.out.print("The first " + multiple + " multiples of " + number + " are: ");
for(int i=1; i<=multiple; i++){
if(i>1){
System.out.print(", ");
}
System.out.print(number*i);
}
}

Not for this site but:
String output = "The first " + multiples + " multiples of " + number + " are: "
for(int x = 1; x <= multiples; x++){
output = output + Integer.toString(x*number) + ",";
}
This can be improved using String.format but ill leave that to you.
Your code could also be improved by doing error checking because right now you could say 'a' for an input and it would crash.
You should look up the basics of programming and also just try stuff you'd be surprised by what you learn just doing random things. There is https://projecteuler.net/ if you need ideas about what to try.

Related

Hackerrank says my output is wrong even though the output matches perfectly [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I am doing an assignment on Hackerrank for Java where I had to perform some mathematical operations with scanner data. I ran the code with two samples and I got a 100% match with the expected output (see picture) but somehow Hackerrank still says there is no match. Is it possible that you print something and it looks the same but it is recognized as something different? I already tried to write the code in a different way so the result would be picked up correctly, by using System.out.println but this was to no avail.
import java.util.*;
import java.io.*;
class Solution {
public static void main(String []argh) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
String jj = "";
for (int i = 0; i < t; i++) {
int a = in.nextInt();
int b = in.nextInt();
int n = in.nextInt();
String hh = "";
for(int k=1;k<n+1;k++){
long oo = a ;
for (double o = 0; o < k; o++) {
oo = oo + b * (long) Math.pow(2, o);
}
hh = hh + " " + oo;
}
if (i > 0) {
System.out.print("\n");
}
System.out.print(hh);
}
in.close();
}
}
image of Hackerrank result
Since there's no picture included to compare your code to output and/or expected output, I can only guess, but might be that System.out.print("\n"); is not visible in html output, but is still picked up by the validation algorythm, and if lets say 5 is expected \n5 would obviously be wrong, even if the \n whitespace part is invisible

How to get a specific character using an integer and if...else [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
We have an assignment about if...else. How do I get the character from a string if we input an integer?
This is for netbeans since it's the only application that was taught to us.
Scanner scan = new Scanner(System.in);
System.out.println("Enter your word: ");
String word = scan.nextLine();
System.out.println("Enter your number: ");
int num = scan.nextInt();
if (word.charAt(num))
{
System.out.println( "Answer is " + word.charAt(0));
}
else ( word.length < num)
{
System.out.println("number exceeds string length");
// the if part is where the confusion began
// index should start from 1 and not 0
Expected Output:
Enter word: Flood Enter number: 2 Answer: l
(If number exceed entered exceeds the output should be)
Enter word: Flood Enter number: 6 number exceeds string length
As array index always starts from 0, you can always subtract by 1 in num. Try the code below:
//if user enter char at 5 then it will num-1=4
if (word.length() > num-1)
{
//value at 4 postion as index starts from 0
System.out.println( "Answer is " + word.charAt(num-1));
}
else
{
System.out.println("number exceeds string length");
}
Output :
Enter your word:
weeet
Enter your number:
5
Answer is t

How do I display the actual input instead of option number? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to get this to display the actual input instead of the option number.
import java.util.*;
public class RandomGenerator {
public static void main(String[] args) {
int length;
Scanner input = new Scanner(System.in);
System.out.println("How many options?"); //user input food options
length = input.nextInt();
String[] names = new String[length];
for(int counter = 0; counter < length; counter++){
System.out.println("Enter option #" + (counter+1) + ":");
names[counter] = input.next();
}
input.close();
System.out.println("You are going to eat " + new Random().nextInt(names.length));
You've already generated a random number here:
new Random().nextInt(names.length)
You can use this random number to access an element in the names array.
int randomNumber = new Random().nextInt(names.length);
String option = names[randomNumber]; // here is the important bit!
Now you can print option out!
System.out.println("You are going to eat " + option);

Infinite multiplication questions

I am trying to make a program that randomly generates two single digit numbers, creates a multiplication question, asks the user for the answer, counts the amount of questions and correct answers, and after every question the user is able to stop the loop to see how many he got correct out of the number of questions that were given.
I believe that I should be using a while loop (probably the easiest way) but I don't know exactly how to go about doing that. Here is what I currently have:
int number = 0;
int number1 = (int)(Math.random() * 10); //Produce two single digit numbers
int number2 = (int)(Math.random() * 10);
Scanner input = new Scanner(System.in); //Create Scanner
System.out.print("What is " + number1 + " + " + number2 + "? ");
int answer = input.nextInt();
while (number <= 10) {
if (number1 + number2 != answer ) {
System.out.println(" Incorrect!\n Would you like to see your overall result? (yes or no)");
}
else {
System.out.println(" Correct, great job!\n Would you like to see your overall result? (yes or no)");
}
}
Since this seems like an assignment I'll just give you pseudocode
MainMethod
{
//Create a new Random to generate random numbers
//(Import java.util.random)
//You need variables for:
//Total number of questions asked
//Total number of correct answers
//Start the question loop
while(true){
//Generate your two random numbers
//Ask the question
//Index the total number of questions
//Get their answer
//Check their answer
if(answer is correct){
//Index correct answers
}
//Ask whether they want to continue
//Check whether they want to stop
if(they want to quit){
//Break out of the while loop (break;)
}
}
//Tell them how many they got correct out of the total
}
Maybe I gave a little too much, but I hope this helps.

Program won't run properly [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
So I wrote this program but for some reason my else statement won't work.
If i input "e" for example my program will simply crash... when its actually supposed to return "Invalid Input"
Can someone please help me?
=========================================================================
import java.util.Scanner;
public class AbsValue2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a number:");
String num = input.nextLine();
Double num2 = new Double(Double.parseDouble(num));
Double abs_val = new Double(Math.sqrt(num2 * num2));
if (num.matches("[+-]?[\\d]+[.]*"))
System.out.println("The absolute value of " + num + " is |" + abs_val + "|");
else if (num.matches("[+-]?[\\d]*.[\\d]+"))
System.out.println("The absolute value of " + num + " is |" + abs_val + "|");
else
System.out.println("Invalid input");
}
}
the problem is here:
Double num2 = new Double(Double.parseDouble(num));
if num is "e", then you will get a NumberFormatException.
You can use Scanner.hasNextDouble{}, to check, or try/catch that exception.
Your code is throwing a NumberFormatException before the if statement is reached, because of this line:
Double num2 = new Double(Double.parseDouble(num));
You need to check the validity before parsing it.

Categories

Resources