本文目录一览:
- 1、Java如何用最有效的方法计算22*8?
- 2、java编程实现任意两个数组的乘法运算
- 3、java乘法运算
- 4、JAVA的加,减,乘,除运算方法
- 5、JAVA 两个数相乘怎么写?
- 6、java 怎么算乘法
Java如何用最有效的方法计算22*8?
Java乘法运算还有位运算符也可以完成乘法运算
左移运算符 ,左移一位,相当于乘2
右移运算符 ,右移一位,相当于除2
22 乘 8=11 乘 16, 16是2的4次方,就是通过位运算符,11向左移4位
22 乘 8最有效的计算方法为: 11左移4位 及114
你明白了吗?
java编程实现任意两个数组的乘法运算
mport java.util.Scanner;
public class liujian {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
A num;
num=new A();
Scanner in = new Scanner(System.in);
System.out.print("输入第一个数:");
num.a = in.nextInt();
System.out.print("输入第二个数:");
num.b = in.nextInt();
System.out.println("两数相加的结果为:");
System.out.println(num.a+num.b);
System.out.println("两数相减的结果为:");
System.out.println(num.a-num.b);
System.out.println("两数相乘的结果为:");
System.out.println(num.a*num.b);
System.out.println("两数相除的结果为:");
System.out.println(num.a/num.b);
System.out.println("两数平方的结果为:");
System.out.println(num.a*num.b);
}
}
class A{
int a,b;
void jjcc(int s){
System.out.println(s);
}
}
java乘法运算
//早前写的,简易的!
import java.util.*;
public class TestSum {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
while(true){
init();
}
}
private static void init() {
System.out.println("请输入算数表达式如:1+1回车即可:");
String str = sc.nextLine();
if (!str.matches("[^a-zA-Z()^#$@!~]+")) {
System.out.println("输入错误重输:请输入算数表达式如:1+1回车即可:");
str = sc.nextLine();
}
int q = 0, h = 0, sum = 0;
char chs = ' ';
for (int i = 0; i str.length(); i++) {
chs = str.charAt(i);
if (chs == '+' || chs == '-' || chs == '*' || chs == '/') {
q = Integer.valueOf(str.substring(0, i));
h = Integer.valueOf(str.substring(i + 1, str.length()));
switch (chs) {
case '+':
sum = q + h;
break;
case '-':
sum = q - h;
break;
case '*':
sum = q * h;
break;
default:
sum = q / h;
break;
}
break;
}
}
System.out.println("结果="+sum+"\n继续下一轮:");
}
}
JAVA的加,减,乘,除运算方法
首先可以把计算器看成是一个对象就是一个类,它有加、减、乘、除功能,这四个就是这个类的方法;你可以给这个类定义两个成员变量
int
x、int
y
然后分别用这四个方法对x
、y
实行加、减、乘、除并返回其值
代码大概如下:
class
counter{
private
int
x;
private
int
y;
public
counter(){
}
public
counter(int
x,int
y){
this.x=x;
this.y=y;
}
public
double
adding(){
//加运算
return
x+y;
}
public
double
minus(){
//减运算
return
x-y;
}
public
double
times(){
//乘运算
return
x*y;
}
public
double
divide(){
//除运算
return
x/y;
}
}
//测试类
public
class
test{
public
static
void
main(string[]
args){
counter
c=new
counter(5,4);//实例化
system.out.println(c.adding());//输出加的结果
system.out.println(c.minus());//输出减的结果
system.out.println(c.times());//输出乘的结果
system.out.println(c.divide());//输出除的结果
}
}
希望对你有帮助
JAVA 两个数相乘怎么写?
public class Day25B {
public static void main(String[] args) {
baiint[] arr1=new int[5],arr2=new int[5],result=new int[5];
for (int i = 0; i result.length; i++) {
arr1[i]=(int)Math.round(Math.random()*40+10);
arr2[i]=(int)Math.round(Math.random()*40+10);
result[i]=arr1[i]*arr2[i];
}
System.out.println("索引\tarr1\tarr2\tresult");
for (int i = 0; i result.length; i++) {
System.out.println(i+"\t"+arr1[i]+" x "+arr2[i]+" =\t"+result[i]);
}
}
}
扩展资料:
javap 类文件反汇编器数据类型boolean 布尔型
byte 字节型
char 字符型
short 短整型
int 整形
long 长整形
float 单精度浮点型
double 双精度浮点型class 类null 空类型
interface 接口
java 怎么算乘法
package CMJqimo;
import java.util.Random;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class test {
static int trueresult = 0;
public static void main(String args[]) {
new test();
}
public test() {
JPanel jp = new JPanel();
JLabel jl = new JLabel();
JTextField jtf;
JFrame jf = new JFrame("Exam of Multiplication");
JButton jb = new JButton();
Container contentPane = jf.getContentPane();
contentPane.add(jp);
contentPane.setLayout(new BorderLayout());
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(300, 200);
jp.setLayout(new FlowLayout());
jp.setBackground(Color.GREEN);
jp.setSize(1000, 1000);
int num1 = random_number();
int num2 = random_number();
trueresult = num1 * num2;
JLabel jll = new JLabel(num1 + " x " + num2 + " =");
jp.add(jll);
jtf = new JTextField(5);
jp.add(jtf);
jb = new JButton("提交");
jp.add(jb);
jl = new JLabel(" ");
jp.add(jl);
contentPane.add(jp);
jf.setLocation(400, 200);
jf.setVisible(true);
jb.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int input = Integer.valueOf(jtf.getText());
String s = output_result(trueresult, input);
JOptionPane.showMessageDialog(jp, s);
int a = random_number();
int b = random_number();
trueresult = a * b;
jll.setText(a + " x " + b + " =");
jtf.setText("");
}
});
}
public static int read_input(String s) {
return Integer.parseInt(s);
};
public static int random_number() {
Random r = new Random();
int num = r.nextInt(10) + 1;
return num;
}
public String output_result(int trueresult, int input) {
Random r = new Random();
if (input == trueresult) {
String[] s = { "Very good", "Excellent", "Great job" };
return s[r.nextInt(3)];
} else {
return "No, please try again";
}
}
}