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 9 years ago.
Improve this question
I want to output 99 multiplication table not use the loop. Do not say only use 'system.out.print'.
public class NextDiGui {
public static void main(String[] args) {
row();
}
static int num1 =0, num2 =1;
public static void row(){
num1++;
if(num1<10){
col();
}
}
public static void col(){
if(num2<=num1){
System.out.print(num1+"*"+num2+"="+num1*num2+"\t");
num2++;
col();
}
else{
num2=1;
System.out.println();
row();
}
}
}
Related
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 1 year ago.
Improve this question
I am new to java, why a, b,c initialization are wrong in the following code.
public static void main(String[] args) {
if (args.length < 2)
throw new IllegalArgumentException ("we need 2 argumeents");
else {
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int c = a+b;
}
System.out.println(a + " + " + b + " = " + c);
}
Java works differently compared to JavaScript. Every {} block has an own variable scope. Variables defined inside a block are not visible outside.
public static void main(String[] args) {
{
int x=1;
System.out.println(x); // prints 1
}
{
int x=2;
System.out.println(x); // prints 2
}
// System.out.println(x); // error: cannot find symbol
}
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
Basically I have to write a program that uses data that was inputted by the user and put it in a mathematical formula so it spits out an answer. The formula has to be created as a method. This is what I have so far. When I run the code it lets me input a number, but when I do that the program does not output anything and it just finishes running.
import java.util.Scanner;
public class Namek {
static int myMethod(int radius) {
return ((radius * radius) * (22/7));
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter Radius");
String radius = keyboard.nextLine();
}
private static void myMethod() {
}
You need to call the method passing the radius as parameter of the method. You can check https://www.tutorialspoint.com/java/java_methods.htm
,this link to see how to create and call methods with parameters.
I think what you wanted to do is the following:
import java.util.*;
public class Namek {
static int myMethod(int radius) {
return ((radius * radius) * (22/7));
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter Radius");
int radius = Integer.valueOf(keyboard.nextLine());
int area = myMethod(radius);
System.out.println(area);
}
}
Below is what you were trying to do I think
public class Formula {
static int myMethod(int radius) {
return ((radius * radius) * (22/7));
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter Radius");
int radius = Integer.valueOf(keyboard.nextLine());
float output = myMethod(radius);
System.out.println(output);
}
}
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 6 years ago.
Improve this question
I made this program on C,and I have no idea of Java (new-yοung developer).Can you help me to "translate" it on Java?
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
void printbin(int num)
{
int binnum;
binnum=num%2;
num=num/2;
if (num!=0) printbin(num);
printf("%d",binnum);
return;
system("pause");
}
int main(void)
{
int posnumber,binnum;
printf("Give a number : ");
scanf("%d",&posnumber);
printbin(posnumber);
printf("\n");
return 0;
}
You can learn from this code, hope this can help you:
//here is comments in java
package help;
import java.util.Scanner;
//this is the way how you can all the libraries in java so
/*
#include<stdio.h>
#include<stdlib.h>
#include<graphics.h>
*/
//here name of your class
public class Help {
//here name of your methode
public static void printbin(int num) {
int binnum;
binnum = num % 2;
num = num / 2;
if (num != 0) {
printbin(num);
}
System.out.println(binnum);
}
//here is the main methode in java
public static void main(String args[]) {
int posnumber, binnum;
Scanner scan = new Scanner(System.in);
System.out.println("Give a number : ");
posnumber = scan.nextInt();
System.out.println(posnumber);
}
}
Good luck, and good start with java :)
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 6 years ago.
Improve this question
I want the age to be multiplied by 3 here is my code asking for the user to enter name and age...............................................
import java.util.Scanner;
class Lab1Part4 {
public static void main (String [] args) {
Scanner user_input =
int tripleAge;
String printMyName;
String firstName;
String secondName;
System.out.println("Please enter your first name ? ");
firstName = user_input.next ();
System.out.println("Please enter your second name? ");
secondName = user_input.next ();
printMyName = firstName + " " + secondName;
System.out.println( printMyName);
System.out.println("Enter your age ? ");
tripleAge = user_input.nextInt ();
}// end class
}// end main
You just need to do something like:
tripleAge = user_input.nextInt () * 3;
System.out.println(tripleAge);
Use * operator to multiply.
tripleAge = (user_input.nextInt () * 3);
System.out.println(tripleAge);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
It gives me following error: The operator == is undefined for the argument type(s)
boolean, int
Syntax error on tokens, delete these tokens
package javaproject;
public class NestedIFandIFandElse {
public static void main(String[] args) {
int vanus = 50;
if (vanus == 40) {
System.out.println("first if ");
} else {
System.out.print("first else");
if (vanus == 50 ∣∣ vanus == 20) {
System.out.println("second if");
} else {
System.out.println("second else");
}
}
}
}
You should change ∣∣ to ||. They look the same, but they aren't:
if (vanus == 50 || vanus == 20)
I'm not certain how you entered that symbol, but ∣∣ is not ||;
if (vanus == 50 || vanus == 20 ) { // <-- The || or works here.
I changed it as above, and it compiles here.