2016-11-26 167 views
0

我正在尝试为输入String的函数编写代码,并将它的余数​​作为“int”除以7时返回。 出于某种原因,我得到了下面的错误,java.math.BigInteger类中的“无法找到符号”错误

Main.java:16: error: cannot find symbol 
     n=java.math.BigInteger.bg.intValue(); 
         ^
    symbol: variable bg 
    location: class BigInteger 
    1 error 

我的代码如下,

/* package whatever; // don't place package name! */ 

    import java.util.*; 
    import java.lang.*; 
    import java.io.*; 

    /* Name of the class has to be "Main" only if the class is public. */ 
    class Ideone 
    { 
     int remainderWith7(String num) 
     { 
      // Your code here 
      java.math.BigInteger bg=new java.math.BigInteger(num); 
      Integer n=java.math.bg.intValue(); 
      //int n=java.util.Integer.parseInt(num); 
      //hello 
      return (int)n%7; 
     } 
     public static void main (String[] args) throws java.lang.Exception 
     { 
      // your code goes here 
      Ideone id=new Ideone(); 
      id.remainderWith7("10"); 
     } 
    } 

请帮助。 谢谢。

+2

'bg'是你的变量只是名字。你为什么试图用'java.math'来限定它?只要使用'Integer n = bg.intValue();'(另外请注意,你提供的代码与你所显示的错误信息不符 - 总是值得确保你的一致性。) –

+0

@JonSkeet谢谢您。这完全奏效。 – iamrkcheers

回答

2

数学中没有属性名为bg

行更改为:

Integer n= bg.intValue(); 
+0

你能不能也告诉输入一些如“56495654565052555054535456545355495650575755555757575350505449495254525056535655494951545354515152515050575749545453535549545551575652535149494949515551545554565555575452555157505555574950505649525149505150575254515549565156515750555450545355495355515251495352565452555453515451505251575251494956515352555154505155535151565754505450535753555654575549575349565351575054”时,输出出来为0,而所需的输出是6?!? – iamrkcheers

+0

检查bg.intValue()的结果可能是7的倍数。您不能使用BigInteger并将其转换为Integer而不丢失数字。 – JFPicard

+0

使用上面的字符串计算代码作为输入引发运行时错误,如下所示:运行时错误\t时间:0.04内存:711168信号:-1' – iamrkcheers

0
import java.math.BigInteger; 
import java.util.Scanner; 

public class BigIntergerSumExample { 

    public static void main(String args[]) { 

     BigInteger number1; 
     BigInteger number2; 
     BigInteger sum; 
     Scanner sc = new Scanner(System.in); 
     System.out.println("Enter the value of number 1"); 
     number1 = sc.nextBigInteger(); 
     System.out.println("Enter the value of number 2"); 
     number2 = sc.nextBigInteger(); 


     BigInteger a = new BigInteger(""+number1); 
     BigInteger b = new BigInteger(""+number2); 
     BigInteger result = a.add(b); 

     System.out.println("Sum is Two numbers : -> " + result); 
    } 

} 


Answer is 

Enter the value of number 1 
1111111111111111111111111111111111111111111111111 
Enter the value of number 2 
2222222222222222222222222222222222222222222222222 
Sum is Two numbers : -> 
3333333333333333333333333333333333333333333333333 
+0

import java.math.BigInteger; –

相关问题