【问题标题】:Turning arrays into an integer (int) when we need to swap pairs of numbers当我们需要交换数字对时将数组转换为整数 (int)
【发布时间】:2015-12-11 14:40:04
【问题描述】:

我已经在 array 上创建了一个方法,现在我正在尝试将 array 转换为 int。有任何想法吗?谢谢!

public Number swapPairs() {
    int n = 0 ; 
    this.num =  n ; 
    int    []a; 

    for (int i=0 ; i==getNumOfDigits();i++){
        a[i]=n%10 ; 
        int k = a[i];
        a[i]=a[i-1];
        a[i-1]=k ;
        a[0]=a[0];



    }
   []a= scr.nextInt();
    Number New = new Number(j);

【问题讨论】:

  • 谢谢大家,我完全改变了它并完成了我的项目,但是由于我是初学者,所以任何人都可以帮助我测试这门课吗?在下一条评论中
  • public Number swapPairs(){ // 这个方法返回后面所有两个数字的交换 String s=""+num , n="";字符 c,t; for (int i = 0; i
  • 如果你要问一个新问题,这应该变成一个新问题。
  • 请为您使用的编程语言添加标签。它看起来像 Java?
  • 是的它的java,我做了一个标签

标签: java arrays eclipse int


【解决方案1】:

在这种情况下,如果要将数组转换为整数,则至少应具有与数组本身相同的变量计数。例如,用户输入了 10 个值,因此数组初始化将从 a[0]a[9]。那么你应该有 10 个变量来保存数组中的值。

例如:

temp1 = a[0];
temp2 = a[1];
temp3 = a[2];
temp4 = a[3];
temp5 = a[4];
temp6 = a[5];
temp7 = a[6];
temp8 = a[7];
temp9 = a[8];
temp10 = a[9];

【讨论】:

    【解决方案2】:

    里面放了什么信息。

    您没有声明数组大小,也没有声明数据类型。

    this.numsrc 在此方法的上下文中没有任何意义。

    【讨论】:

      【解决方案3】:
      // this method returns the swap of all two numbers that behind of it  
      public Number swapPairs(){         
          String s=""+num , n="";
          char c,t;
      
          for (int i = 0; i<getNumOfDigits(); i+=2) {
              if(i+1<getNumOfDigits()) {
                  c=s.charAt(i);
                  t=s.charAt(i+1);
                  n+=t+""+c;
              }
          }
      
          if(getNumOfDigits()%2==0) { 
              int nam = Integer.parseInt(n);
              Number num = new Number (nam);
              return num;              
          } else {
              n=n+s.charAt(getNumOfDigits()-1);
              int nam = Integer.parseInt(n);
              Number num = new Number ( nam);
              return num; 
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-06-07
        • 2021-07-01
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-17
        • 2011-12-23
        相关资源
        最近更新 更多