小编Par*_*han的帖子

简化Java中的分数

我的任务是发展一个理性的阶级.如果500和1000是我的输入,则(½)必须是我的输出.我自己写了一个程序来找到它.

还有另一种找到解决方案的最佳方法,或者我的程序已经是最好的了吗?

public class Rational {

    public static void main(String[] args){

       int n1 = Integer.parseInt(args[0]);
       int n2 = Integer.parseInt(args[1]); 
       int temp1 = n1;
       int temp2 = n2; 

       while (n1 != n2){
         if(n1 > n2)
            n1 = n1 - n2;
         else
            n2 = n2 - n1;
       }      

      int n3 = temp1 / n1 ;
      int n4 = temp2 / n1 ;

      System.out.print("\n Output :\n");

      System.out.print(n3 + "/" + n4 + "\n\n" );
      System.exit(0);
    }  
}
Run Code Online (Sandbox Code Playgroud)

java rational-numbers simplification

21
推荐指数
3
解决办法
5万
查看次数

Java中有任何goto语句吗?

我用C++编写了一个带有一些goto语句的程序.现在,我需要用Java编写相同的程序.Java中有没有goto选项?如果是这样,它是如何实现的?它和C++一样吗?

我的计划是:

#include<stdio.h>
#include<conio.h>

void main()
{
    int i,j,k,w,l,q,d;
    clrscr();
    printf("\nEnter the limit:");
    scanf("%d",&k);
    for(i = 13; i <= k ; i++)
    {
        repeat:
        j = i%10;
        if (j != 0)
        {
            if(i < 99)
            {
                for(w = 1; w <= 9; w++)
                {
                    l = 11*w;
                    if (l == i){
                        i++;
                        goto repeat;
                    }
                }
            }
            if(i > 99)
            {
                for(q = 1; q<=9 ; q++)
                {
                    d = 111*q;
                    if (d == i){
                        i++;
                        goto repeat;
                    }
                } …
Run Code Online (Sandbox Code Playgroud)

c++ java goto

0
推荐指数
1
解决办法
1801
查看次数

标签 统计

java ×2

c++ ×1

goto ×1

rational-numbers ×1

simplification ×1