如何在不使用模运算符的情况下找到除以两个数的余数!! 我的老师给了我这个精确的练习,这是我在一门名为编程基础的课程中的第五讲.
我已经尝试过这个等式,
a%b = a - (a/b)*b
Run Code Online (Sandbox Code Playgroud)
但它总是返回零!
String b = "5A";
int bConv = Integer.parseInt(b, 16);
char $2 = bConv;
Run Code Online (Sandbox Code Playgroud)
当我尝试这个时,我可能会失去精度错误警告.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class Tix extends JFrame {
private static final int WIDTH = 500;
private static final int HEIGHT = 500;
private boolean xTurn = true;
private Font style;
private static JButton[][] btns = new JButton[3][3];
public Tix() {
setTitle("Tix");
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(WIDTH, HEIGHT);
createContents();
setVisible(true);
}
public void createContents() {
style = new Font("Comic Sans", 1, 100);
Listener listener = new Listener();
setLayout(new GridLayout(3,3));
for (int i = 0; i < 3; i++) { …Run Code Online (Sandbox Code Playgroud) 该程序执行以下操作:第一个输入从用户获得两个数字,第二个输入如果用户输入(+)符号,程序将两个数字相加,如果用户输入( - )符号,则从第二个数字中减去第一个数字.但没有结果显示该程序只是运行和终止.
import javax.swing.JOptionPane;
public class ObtainNumber {
public static void main(String []args) {
String strNum1 = JOptionPane.showInputDialog("Enter the first number");
String strNum2 = JOptionPane.showInputDialog("Enter the second number");
int num1 = Integer.parseInt(strNum1);
int num2 = Integer.parseInt(strNum2);
String askForOperation = JOptionPane.showInputDialog("What operation needs to be done?");
int sum;
if (askForOperation == "+") {
sum = num1 + num2;
JOptionPane.showMessageDialog(null, "The result of the Addition is " + sum);
}
double subtract;
if (askForOperation == "-") {
subtract = num2 - …Run Code Online (Sandbox Code Playgroud)