How to call a method from another method to get the withdrawal value divide into $20 notes and $50 notes

huangapple go评论69阅读模式
英文:

How to call a method from another method to get the withdrawal value divide into $20 notes and $50 notes

问题

import java.util.Scanner;

public class Main {
    public static int Input() {
        Scanner input = new Scanner(System.in);
        int result;
        return result = input.nextInt();
    }

    static void getValue() {
        int money = Input();
        int bill20, bill50;
        int x = 0, y = 0;
        bill50 = (money / 50);
        for (int i = 1; i <= (money / 50); i++) {
            y++;
        }
        bill20 = (money - (y * 50)) / 20;
        for (int i = 1; i <= bill20; i++) {
            x++;
        }
        System.out.println("Here is " + x + " $20 notes and " + y + " $50 notes.");
    }

    public static void run() {
        System.out.print("Enter the amount of money: ");
        int money = Input();
        int bill20, bill50;
        int x = 0, y = 0;
        if (money > 100 && money % 50 != 0 && money % 20 != 0) {
            bill50 = (money / 50) - 1;
            for (int i = 1; i <= bill50; i++) {
                y++;
            }
            bill20 = (money - (y * 50)) / 20;
            for (int i = 1; i <= bill20; i++) {
                x++;
            }
            int sum = (x * 20) + (y * 50);
            if (sum == money) {
                System.out.println("Here is " + x + " $20 notes and " + y + " $50 notes.");
            } else {
                getValue(); // Something went wrong here
            }
        }
    }

    public static void main(String[] args) {
        run();
    }
}

输出:

Enter the amount of money: 110
Here is 3 $20 notes and 1 $50 notes.
Enter the amount of money: 190
Here is 2 $20 notes and 3 $50 notes.

当输入 190 时,系统会输出 "Here is 2 $20 notes and 3 $50 notes."。

英文:

currently I'm working on an assignment where I take the user's input, which is the amount of money that they want to withdraw and the output will be that money divide into bills of 20 and bills of 50. I have complete the conditions for other cases but I got stuck here, with numbers 110 and 190. I'm unable to call the getValue method from the run method, can you guys help me

 import java.util.Scanner;
public class Int{
public static int Input(){
Scanner input = new Scanner(System.in);
int result;
return result= input.nextInt();
}
static void getValue(){
int money = Input();
int bill20, bill50;
int x = 0, y = 0;
bill50 = (money/50);
for(int i=1; i&lt;= (money/50); i++){
y++;
}
bill20 = (money-(y*50))/20;
for(int i=1; i&lt;=bill20; i++){
x++;
}
System.out.println(&quot;Here is &quot; + x + &quot; $20 notes and &quot;+y+ &quot; $50 notes. &quot;);
}
public static void run(){
System.out.print(&quot;Enter the amount of money: &quot;);
int money = Input();
int bill20, bill50;
int x = 0, y = 0;
if(money &gt; 100 &amp;&amp; money % 50 != 0 &amp;&amp; money % 20 != 0 ){
bill50 = (money/50)-1;
for(int i=1; i&lt;=bill50; i++){
y++;
}
bill20 = (money-(y*50))/20;
for(int i=1; i&lt;=bill20; i++){
x++;
}
int sum = (x*20)+(y*50);
if (sum == money){
System.out.println(&quot;Here is &quot; + x + &quot; $20 notes and &quot;+y+ &quot; $50 notes. &quot;);
} else{
getValue(); //Something went wrong here
}
}
}
//Printing the run method
public static void main(String[] args){
run();
}
}

Output:

Enter the amount of money: 110
Here is 3 $20 notes and 1 $50 notes.

Enter the amount of money: 190

When I entered 190, the system didn't print anything

答案1

得分: 0

以下是您要翻译的代码内容:

Buddy你的问题已经解决了请尝试这段代码

import java.util.Scanner;

class Int {
    int result;
    
    public void Input() {
        System.out.printf("输入金额:");
        Scanner input = new Scanner(System.in);
        result = input.nextInt();
    }
    
    public void getValue() {
        int money = result;
        int bill20, bill50;
        int x = 0, y = 0;
        bill50 = (money / 50);
        for (int i = 1; i <= (money / 50); i++) {
            y++;
        }
        bill20 = (money - (y * 50)) / 20;
        for (int i = 1; i <= bill20; i++) {
            x++;
        }
        System.out.println("这里有 " + x + " 张 20 美元钞票和 " + y + " 张 50 美元钞票。");
    }
    
    public void run() {
        System.out.print("输入金额:");
        int money = result;
        int bill20, bill50;
        int x = 0, y = 0;
        if (money > 100 && money % 50 != 0 && money % 20 != 0) {
            bill50 = (money / 50) - 1;
            for (int i = 1; i <= bill50; i++) {
                y++;
            }
            bill20 = (money - (y * 50)) / 20;
            for (int i = 1; i <= bill20; i++) {
                x++;
            }
            int sum = (x * 20) + (y * 50);
            if (sum == money) {
                System.out.println("这里有 " + x + " 张 20 美元钞票和 " + y + " 张 50 美元钞票。");
            } else {
                getValue(); // 这里出了些问题
            }
        }
    }
    
    public static void main(String[] args) {
        Int i = new Int();
        i.Input();
        i.getValue();
        i.run();
    }
}
英文:

Buddy your issue is fixed, try this code

import java.util.Scanner;
class Int{
int result;
public void Input(){
System.out.printf(&quot;Enter the money : &quot;);
Scanner input = new Scanner(System.in);
result= input.nextInt();
}
public void getValue(){
int money = result;
int bill20, bill50;
int x = 0, y = 0;
bill50 = (money/50);
for(int i=1; i&lt;= (money/50); i++){
y++;
}
bill20 = (money-(y*50))/20;
for(int i=1; i&lt;=bill20; i++){
x++;
}
System.out.println(&quot;Here is &quot; + x + &quot; $20 notes and &quot;+y+ &quot; $50 notes. &quot;);
}
public void run(){
System.out.print(&quot;Enter the amount of money: &quot;);
int money = result;
int bill20, bill50;
int x = 0, y = 0;
if(money &gt; 100 &amp;&amp; money % 50 != 0 &amp;&amp; money % 20 != 0 ){
bill50 = (money/50)-1;
for(int i=1; i&lt;=bill50; i++){
y++;
}
bill20 = (money-(y*50))/20;
for(int i=1; i&lt;=bill20; i++){
x++;
}
int sum = (x*20)+(y*50);
if (sum == money){
System.out.println(&quot;Here is &quot; + x + &quot; $20 notes and &quot;+y+ &quot; $50 notes. &quot;);
} else{
getValue(); //Something went wrong here
}
}
}
//Printing the run method
public static void main(String[] args){
Int i = new Int();
i.Input();
i.getValue();
i.run();
}
}

huangapple
  • 本文由 发表于 2020年9月25日 10:27:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/64056919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定