#I need help again with basic coding again guys
444 messages ยท Page 1 of 1 (latest)
โ This post has been reserved for your question.
Hey @stone basalt! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
I have a assignemnt
here are the parameters etc
im very confused on the subject
so idk
it needs to do those things
Are you even allowed to name a non main method main
./run java
public class Main {
public static int main() {return 1;}
public static void main(String[] args) {}
}
Your java(15.0.2) code ran without output @cosmic vortex
idk im confused
oooooh
so the main method
cant be liek tyhat
i have to make another mthod inside of it>
buttt
it needs to contain 2 int
so
Sorry I was wrong you can name another method main
One that is void and takes in String[]
huh
your killing me
laymans terms pls
als
also
wahts the line to inpout util scanner
When they say main method holds that doesn't mean change the parameter list
So actually your your question is asking you to create two variables inside the main method
or something
Not actually change the main method to take in two ints
NO my question is how to do the assignemnt
Yes I'm trying to help you with the question
Because you messed up on the first part
alr
All I wants you to do is create two variables in the main method that are ints
For the first bit
And yeah you do want to Scanner her for this
It would be java.util.Scanner
Idk what all that is tho lol
no
Good
dont i input it?
like
java.util.Scanner.input
or somethoing
idk
gimme one sec
Import the package? Yes
The package name itself is java.util.Scanner
Something tells me you're mixing up the keyword import into the actual package name
Yes
Looks good
Yep
It helps if you just copy and paste the code in discord
It does look good though onto the last part
f(x)
So you don't create the method in the main method
You call it and it returns a value or does something
yeah a function inside of a function cant exsist
Technically it can but for this yeah
so where do i make it then
So create the method in the class, the same spot as where the main method is
but outside its brackets right
import java.util.Scanner;
public class NumbersDemo{
public static void main(String[] args){
int a;
int b;
Scanner inputDevice = new Scanner (System.in);
System.out.println("Enter an integer >> ");
a = inputDevice.nextInt();
System.out.println("Enter another integer >> ");
b = inputDevice.nextInt();
}
public static void main(String[] args){
}
}
like this
?
public class MainClass {
public static void main(String[] args) {
}
// Your methods here
}
sooo
Look here
like i did?
See those 3 ``` at the top of my message
yes
Add them at the bottom too and the message becomes this:
.
Nice
Your method is in the right spot just change the name and parameters
import java.util.Scanner;
public class NumbersDemo{
public static void main(String[] args){
int a;
int b;
Scanner inputDevice = new Scanner (System.in);
System.out.println("Enter an integer >> ");
a = inputDevice.nextInt();
System.out.println("Enter another integer >> ");
b = inputDevice.nextInt();
}
public static void displayTwiceTheNumber(String[] args){
}
public static void displayNumberPlusFive(String[] args){
}
public static void displayNumberSquared(String[] args){
}
}
wym
ah yes
it cant be void right
it has to be return
or something
Well are they supposed to take in a String array?
so change the parameter list to match that
Just int
import java.util.Scanner;
public class NumbersDemo{
public static void main(String[] args){
int a;
int b;
Scanner inputDevice = new Scanner (System.in);
System.out.println("Enter an integer >> ");
a = inputDevice.nextInt();
System.out.println("Enter another integer >> ");
b = inputDevice.nextInt();
}
public static void displayTwiceTheNumber(int){
}
public static void displayNumberPlusFive(int){
}
public static void displayNumberSquared(int){
}
}
?
The parameter needs a name so you can reference it inside the method
You don't name it a value
letter
Give it a name like num or something
Sure ig
import java.util.Scanner;
public class NumbersDemo{
public static void main(String[] args){
int a;
int b;
Scanner inputDevice = new Scanner (System.in);
System.out.println("Enter an integer >> ");
a = inputDevice.nextInt();
System.out.println("Enter another integer >> ");
b = inputDevice.nextInt();
}
public static void displayTwiceTheNumber(int x){
}
public static void displayNumberPlusFive(int y){
}
public static void displayNumberSquared(int z){
}
}
this
import java.util.Scanner;
public class NumbersDemo{
public static void main(String[] args){
int a;
int b;
Scanner inputDevice = new Scanner (System.in);
System.out.println("Enter an integer >> ");
a = inputDevice.nextInt();
System.out.println("Enter another integer >> ");
b = inputDevice.nextInt();
}
public static void displayTwiceTheNumber(int x){
int q;
int w;
}
public static void displayNumberPlusFive(int y){
int e;
int r;
}
public static void displayNumberSquared(int z){
int t;
int u;
}
}
like this maybe
im confused and stressed this is due at 11 tn
i have 25 minutes to understand the concept
ye
no cause basically they have to multiply and stuff the values i give them tho
That value is passed into the function
so x represents the int values i put in for a and b in the main function?
For instance f(x) = x+5 -> f(5) = 5+5
Make sense?
This idea is directly translated into java methods
public class Main {
public static int f(int x) {
return x+5;
}
}
do you see how this is working?
It's the exact same function but as a Java method
i was making int q
to give it a value of 2
to multiply by it
isnt that whats supposed to happen?
Yep and guess what. To call the method there is quite literally a 1 to 1 mapping
f(5);
this calls the method and returns the value
The name doesn't really matter
Math.pow(n, 2);
So just multiple it by itself
shit
right
import java.util.Scanner;
public class NumbersDemo{
public static void main(String[] args){
int a;
int b;
Scanner inputDevice = new Scanner (System.in);
System.out.println("Enter an integer >> ");
a = inputDevice.nextInt();
System.out.println("Enter another integer >> ");
b = inputDevice.nextInt();
}
public static void displayTwiceTheNumber(int x){
int q = 2;
}
public static void displayNumberPlusFive(int y){
int e = 5;
}
public static void displayNumberSquared(int z){
}
}
so
how do i import values
im runnign out of tim
Okay list
To call the method you need to write displayTwiceNumber(a); in main method
You pass in the value
That you want the method to use
And in your method you are displaying twice the number
So display 2*x
In displayTwiceNimber
You want to display it
no no
How do you print something in Java?
If you're talking about the expression itself, yes
it has to be like this
Okay I see
so it wants me to call the method after it multiplies int a and b and then print it
yes
Yes
To call each of the methods on the first input and the second input
System.out.println(a + "times" + b + "is" )
wait
so i do this
but
what do i put
for the final output
displayTwiceTheNumber?
x is your number
Well you passed in the value in a
And that value is assigned to x
(that's how methods work)
i no understand
This is why we have parameters
Whenever you call a method, the value you send, even if it's a variable, is assigned to the parameter
It's like a new program has started basically and it doesn't know about a, only x because it's defined in the method
So x is the original value. Idk what b is but that should just be 2
import java.util.Scanner;
public class NumbersDemo{
public static void main(String[] args){
int a;
int b;
Scanner inputDevice = new Scanner (System.in);
System.out.println("Enter an integer >> ");
a = inputDevice.nextInt();
System.out.println("Enter another integer >> ");
b = inputDevice.nextInt();
displayTwiceNumber(a);
displayTwiceNumber(b);
System.out.println(a + " times 2 is " + );
System.out.println(a + " plus 5 is " + );
System.out.println(a + " squared is " + );
System.out.println(b + " times 2 is " + );
System.out.println(b + " plus 5 is " + );
System.out.println(b + " squared is " + );
}
public static void displayTwiceTheNumber(int x){
int q = 2;
}
public static void displayNumberPlusFive(int y){
int e = 5;
}
public static void displayNumberSquared(int z){
}
}
no
cause when it grades it right
Ahhhh
Put your print statements in your new method
You are calling these methods and they are printing
Not the main method
so put them in their respective methods?
import java.util.Scanner;
public class NumbersDemo{
public static void main(String[] args){
int a;
int b;
Scanner inputDevice = new Scanner (System.in);
System.out.println("Enter an integer >> ");
a = inputDevice.nextInt();
System.out.println("Enter another integer >> ");
b = inputDevice.nextInt();
displayTwiceNumber(a);
displayTwiceNumber(b);
displayNumberPlusFive(a);
displayNumberPlusFive(b);
displayNumberSquared(a);
displayNumberSquared(b);
}
public static void displayTwiceTheNumber(int x){
int q = 2;
System.out.println(a + " times 2 is " + x );
System.out.println(b + " times 2 is " + x );
x = a*q
x = b*q
}
public static void displayNumberPlusFive(int y){
int e = 5;
System.out.println(a + " plus 5 is " + y );
System.out.println(b + " plus 5 is " + y );
}
public static void displayNumberSquared(int z){
System.out.println(b + " times 2 is " + z );
System.out.println(b + " squared is " + z );
}
}
like this or no
i only did that first one idk if its right
You don't need to declare any new variables in these methos
Your parameters have everything you need
that works?????
Yes
Make sure when you do arithmetic, well, at least when adding that you put it in a parenthesis if it's in a print statement, also order of operations applies (hopefully that's obvious)
import java.util.Scanner;
public class NumbersDemo{
public static void main(String[] args){
int a;
int b;
Scanner inputDevice = new Scanner (System.in);
System.out.println("Enter an integer >> ");
a = inputDevice.nextInt();
System.out.println("Enter another integer >> ");
b = inputDevice.nextInt();
displayTwiceNumber(a);
displayTwiceNumber(b);
displayNumberPlusFive(a);
displayNumberPlusFive(b);
displayNumberSquared(a);
displayNumberSquared(b);
}
public static void displayTwiceTheNumber(int x){
System.out.println(a + " times 2 is " + x );
System.out.println(b + " times 2 is " + x );
x = a*2;
x = b*2;
}
public static void displayNumberPlusFive(int y){
System.out.println(a + " plus 5 is " + y );
System.out.println(b + " plus 5 is " + y );
y = a+5;
y = b+5;
}
public static void displayNumberSquared(int z){
System.out.println(b + " times 2 is " + z );
System.out.println(b + " squared is " + z );
z = a*a;
z = b*b;
}
}
now im very confused whats wrong
just so it inline
what
You're confusing yourself by reassigning
You can do arithmetic expressions in a print statement
im confused now
HOW
๐ฆ
Read
Then just change it x and stop asking questions lmao
CHANGE WUT TO X
A and B
x = x^2????
x * x
import java.util.Scanner;
public class NumbersDemo{
public static void main(String[] args){
int a;
int b;
Scanner inputDevice = new Scanner (System.in);
System.out.println("Enter an integer >> ");
a = inputDevice.nextInt();
System.out.println("Enter another integer >> ");
b = inputDevice.nextInt();
displayTwiceNumber(a);
displayTwiceNumber(b);
displayNumberPlusFive(a);
displayNumberPlusFive(b);
displayNumberSquared(a);
displayNumberSquared(b);
}
public static void displayTwiceTheNumber(int x){
System.out.println(a + " times 2 is " + x*2 );
System.out.println(b + " times 2 is " + x*2 );
x = a*2;
x = b*2;
}
public static void displayNumberPlusFive(int y){
System.out.println(a + " plus 5 is " + y+5 );
System.out.println(b + " plus 5 is " + y+5 );
y = a+5;
y = b+5;
}
public static void displayNumberSquared(int z){
System.out.println(b + " times 2 is " + z*z );
System.out.println(b + " squared is " + z*z );
z = a*a;
z = b*b;
}
}
like this
??
:/
Replace It with x y or z
YES you called it twice
Each time it assigns it to x and runs the code
import java.util.Scanner;
public class NumbersDemo{
public static void main(String[] args){
int a;
int b;
Scanner inputDevice = new Scanner (System.in);
System.out.println("Enter an integer >> ");
a = inputDevice.nextInt();
System.out.println("Enter another integer >> ");
b = inputDevice.nextInt();
displayTwiceNumber(a);
displayTwiceNumber(b);
displayNumberPlusFive(a);
displayNumberPlusFive(b);
displayNumberSquared(a);
displayNumberSquared(b);
}
public static void displayTwiceTheNumber(int x){
System.out.println(a + " times 2 is " + x*2 );
System.out.println(b + " times 2 is " + x*2 );
x = x*2;
x = x*2;
}
public static void displayNumberPlusFive(int y){
System.out.println(a + " plus 5 is " + y+5 );
System.out.println(b + " plus 5 is " + y+5 );
y = y+5;
y = y+5;
}
public static void displayNumberSquared(int z){
System.out.println(b + " times 2 is " + z*z );
System.out.println(b + " squared is " + z*z );
z = z*z;
z = z*z;
}
}
lik this
You didn't change anything.....
You still have a
You still have b
In your method that are printing
AHHHH
it FUCKIN WORKED
but
not in the right order
:/
import java.util.Scanner;
public class NumbersDemo{
public static void main(String[] args){
int a;
int b;
Scanner inputDevice = new Scanner (System.in);
System.out.println("Enter an integer >> ");
a = inputDevice.nextInt();
System.out.println("Enter another integer >> ");
b = inputDevice.nextInt();
displayTwiceTheNumber(a);
displayTwiceTheNumber(b);
displayNumberPlusFive(a);
displayNumberPlusFive(b);
displayNumberSquared(a);
displayNumberSquared(b);
}
public static void displayTwiceTheNumber(int x){
System.out.println(x + " times 2 is " + x*2 );
System.out.println(x + " times 2 is " + x*2 );
x = x*2;
x = x*2;
}
public static void displayNumberPlusFive(int y){
System.out.println(y + " plus 5 is " + y+5 );
System.out.println(y + " plus 5 is " + y+5 );
y = y+5;
y = y+5;
}
public static void displayNumberSquared(int z){
System.out.println(z + " times 2 is " + z*z );
System.out.println(z + " squared is " + z*z );
z = z*z;
z = z*z;
}
}
NO IT DIDNT WORK
I TROLD YOU
Okay you do realize that code runs one line at a time right
You only need one print statement in the entire method for each
I didn't see that but yeah that's a big blunder loll
You only need one print statement
Listen
A method is a task
When you call a method you are telling it to print something twice by having two print statements
That's what you just did
Does that make any sense?
A METHOD IS A THING THAT DOES SOMETHING
does that not make sense?!
YES
I love how I said this already
import java.util.Scanner;
public class NumbersDemo{
public static void main(String[] args){
int a;
int b;
Scanner inputDevice = new Scanner (System.in);
System.out.println("Enter an integer >> ");
a = inputDevice.nextInt();
System.out.println("Enter another integer >> ");
b = inputDevice.nextInt();
displayTwiceTheNumber(a);
displayTwiceTheNumber(b);
displayNumberPlusFive(a);
displayNumberPlusFive(b);
displayNumberSquared(a);
displayNumberSquared(b);
}
public static void displayTwiceTheNumber(int x){
System.out.println(x + " times 2 is " + x*2 );
x = x*2;
}
public static void displayNumberPlusFive(int y){
System.out.println(y + " plus 5 is " + y+5 );
y = y+5;
}
public static void displayNumberSquared(int z){
System.out.println(z + " squared is " + z*z );
z = z*z;
}
}
oh shit
naw
still deosnt work
Wdym โ ๏ธ
nbm
nvm
i put this y +(5)
im a dumbass
public class NumbersDemo{
public static void main(String[] args){
int a;
int b;
Scanner inputDevice = new Scanner (System.in);
System.out.println("Enter an integer >> ");
a = inputDevice.nextInt();
System.out.println("Enter another integer >> ");
b = inputDevice.nextInt();
displayTwiceTheNumber(a);
displayTwiceTheNumber(b);
displayNumberPlusFive(a);
displayNumberPlusFive(b);
displayNumberSquared(a);
displayNumberSquared(b);
}
public static void displayTwiceTheNumber(int x){
System.out.println(x + " times 2 is " + x*2 );
x = x*2;
}
public static void displayNumberPlusFive(int y){
System.out.println(y + " plus 5 is " + (y+5) );
y = y+5;
}
public static void displayNumberSquared(int z){
System.out.println(z + " squared is " + z*z );
z = z*z;
}
}
Okay in programming if I do
a = 8
print a
And that a = 6
DO YOU THINK IT'S GOING TO MAGICALLY PRINT 6?????
It's going to print 8
Okay good job
It's going to print it's value
Code runs in a sequential manner
If you print the value it's going to print it's present value
Reassigning it later isn't going to change the print
Because it already happend
hey man
i hope you know
ill be back next tuesday night crying about more assignments that i dont know how to do
f(x) !!!
wait
i think i understand
methods are like functions
like
a piecewise
I can
give it inputs from the main function
or main method in this case
and it can run them and return values to the main method
or it can keep the values and in this case print them
or graph them in math
yes?
but i cant directly use variables through the main function
i have to define them through in my case x
i need help with creating a website
#โ๏ธฑhow-to-get-help not your thread.
methods (programming) are a subset of functions (programming), which are derived from functions (math).