小编Jas*_*n C的帖子

有人可以向我解释此Java代码(使用System.out.format格式化输出)吗?

枚举:

public enum tuna {
Veyron("Speed", "R1"),
Reventon("Speed", "R3"),
MclarenF1("Speed", "R3");

public final String style;
public final String theclass;

tuna(String thestyle, String theclasss){
style = thestyle;
theclass = theclasss;
  }
    public String getStyle(){
            return style;
    }

    public String getClasss(){
            return theclass;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我不理解的主类中的代码:

int maxlength = tuna.Veyron.name().length();

    for( tuna cars : tuna.values() ) {
      System.out.format( "%-" + maxlength + "s   %-5s %5s\n", cars.name(), cars.getStyle(), cars.getClasss() );
Run Code Online (Sandbox Code Playgroud)

但是我不了解的部分(很好)是:

"%-" + maxlength + "s   %-5s %5s\n"
Run Code Online (Sandbox Code Playgroud)

它似乎

%-5s
Run Code Online (Sandbox Code Playgroud)

在汽车名称(cars.name())和 …

java

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

将链表的头部移动到尾部

我需要用 Java 编写一个方法,将链表中的第一个元素移动到最后一个位置。

为了实现这一点,我相信我必须设置一个节点来引用 head 之后的第一个元素,然后将下一个节点设置为 null。我尝试用我的方法执行此操作,但是运行该方法时,输出不正确。

我的课程的其余部分很可能太大,无法发布在这里,但我认为我只需要帮助概念化如何将第一个元素移动到列表的末尾。

我写的方法是:

public void moveFirstToEnd() {
    if (head.next == null) {
        throw new NoSuchElementException();
    } 

    Node node = head.next;
    node.next = null;
    head.next = node;
    tail.next = node;
    tail = node;
}
Run Code Online (Sandbox Code Playgroud)

java linked-list

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

按逆时针方向对3D多边形顶点进行排序

我必须找到多边形的法线,但为了做到这一点,我首先必须按逆时针方向对顶点(x,y,z)进行排序.假设我给出了用户输入的n个顶点数,我如何按逆时针方向对顶点进行排序以将它们变成多边形?

c++ opengl math polygon

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

Java中的每个子类都继承自两个类(Object及其超类)?

当我创建一个新类时,它就是

ClassA extends Object{
}
Run Code Online (Sandbox Code Playgroud)

所以,如果我要从ClassB继承ClassA,那就不是了

ClassA extends Object extends ClassB {
}
Run Code Online (Sandbox Code Playgroud)

这基本上是Java中不允许的多重继承.

或者对于Object类是否有一个特殊的例外,这意味着确实可以安全地说,Java中的每个基类都只从2个类继承

java oop inheritance multiple-inheritance

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

为什么我的 rebase 没有做任何事情?

这个项目只有一个master分支,所以所有的工作都在那里完成。

我错误地犯了一个错字,虽然我可以提交撤消,但我想我会尝试完全删除提交。

提交哈希是dbcbf96b,并且ded82215是它之前的提交。因此,根据这些说明,我这样做了:

  1. git rebase -i ded82215
  2. 这在编辑器中显示了“待办事项”列表,如预期的那样,仅包含以下内容:

    pick dbcbf96 Writer update.
    
    Run Code Online (Sandbox Code Playgroud)
  3. 大概是按照该生成文件的注释中的说明,我将该行更改为:

    drop dbcbf96 Writer update.
    
    Run Code Online (Sandbox Code Playgroud)
  4. 我保存了文件并关闭了编辑器。然后它说:

    Successfully rebased and updated refs/heads/master.
    
    Run Code Online (Sandbox Code Playgroud)
  5. 然后我检查了 GitLab(我用来托管它的东西)希望看到提交消失了,但事实并非如此,它仍然存在。

  6. 我确实git status查看了它推荐的操作并产生了:

    On branch master
    Your branch is behind 'origin/master' by 1 commit, and can be fast-forwarded.
      (use "git pull" to update your local branch)
    nothing to commit, working tree clean
    
    Run Code Online (Sandbox Code Playgroud)
  7. 这有点道理,但至于变基,我不确定发生了什么。现在我确实git pull更新了本地存储库,但一切都刚刚回到我开始dbcbf96b的地方,回到本地文件仍然包含更改,本地存储库是最新的,并且 GitLab 仍然显示修订版。该pull命令产生:

    Updating ded8221..dbcbf96
    
    Run Code Online (Sandbox Code Playgroud)

所以我的 rebase …

git rebase git-rebase

1
推荐指数
2
解决办法
4912
查看次数

设定精度不正常

#include <iostream>
using namespace std;

int main()
{
    cout.precision(50);
    cout<<"Anmol's Square Root Calculator!"<<endl;
    cout<<""<<endl;
    cout << "This program will compute the square root\n";
    cout << "of a number using the Babylonian algorithm!\n";
    cout<<""<<endl;
    cout<<"Only positive numbers work with this algorithm!"<<endl;
    cout<<""<<endl;
    cout<<"All cycle #1 approximate square roots are guessed\n"<<"using 1 as the first        approximate."<<endl;
    cout<<""<<endl;
    cout<<""<<endl;
    cout<<""<<endl;



    char playAgain='y';
    while (playAgain !='n') 
    {
        int count(25), cycle(1);
        double guess, sqrt, num; 
        cout << "Please enter the number you would like to …
Run Code Online (Sandbox Code Playgroud)

c++ precision decimal

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

Java中的进口

我已经读过,与C/C++不同,当我们"导入"包时,它们实际上并没有被添加(它们被引用而不是嵌入)到类文件中.

这有什么性能影响?

我很好奇可能会因为引用的数据(来自包)在.class本身中不可用而产生的开销(调用引用的包等).

或者开销非常小?

java

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

java中的素数测试

public static boolean isPrime(int number)
{
  boolean result = true;

  for (int i=2; i < number/2; i++)
  {
     if (number%i == 0)
     {
        result = false;
     }
  }

  return result;
}
Run Code Online (Sandbox Code Playgroud)

这段代码有效,但是如何修改它,以便当int number = 0时,它返回false?当number = 0时,它返回true.

java

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

在toString方法中抛出两个NullPointerExceptions

如何NullPointerException以相同的方法抛出两个?这就是我现在所拥有的,但第二次投掷导致错误"无法访问的声明"

public String toString() {
    if (rank == null || suit == null) {
        throw new NullPointerException ("Rank cannot be null");
        throw new NullPointerException ("Suit cannot be null");
    }
    return rank + suit;
}
Run Code Online (Sandbox Code Playgroud)

java exception throw

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

Android:NullPointerException创建新的Intent

在创建新的Intent对象时,我总是得到Nullpointer异常.我的目的是创建一个新的Activity,它以"onCreate"方法打开一个新的View.

这是我的主要活动:

public class MainActivity extends Activity {
    public void activateTimer(){
        // some code ....           
        MyTimer timer = new MyTimer(MainActivity.this);
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的Timer类:

public class MyTimer extends MainActivity{
    private Context contextMain;
    public MyTimer(Context context){
        this.contextMain = context;
        // some more code ...
        openMyActivity(contextMain); 
}
public void openMyActivity(Context contextMain){
    // some code ...
    // The next line throws the NPE!
Intent intent = new Intent(contextMain, MyNotificationActivity.class);
this.startActivity(intent);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的活动应该创建:

public class MyNotificationActivity extends Activity {  
    @Override
    public void onCreate(Bundle savedInstanceState) …
Run Code Online (Sandbox Code Playgroud)

java android nullpointerexception

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