Java小程序,求100以内的合数

Java小程序,求100以内的合数,第1张

// 循环100以内的数(除去1)

for (int n = 2 n <= 100 n++)

{

// 循环相除2-自身一半的数

for (int i = 2 i <= n/2 i++)

{

// 如果找到一个数能整除,则为合数

if (n % i == 0)

{

System.out.println(n)

// 跳出循环

break

}

}

}

package com.erayt.ctms.Zimport

import java.io.FileInputStream

import java.io.IOException

import java.io.InputStream

import java.lang.reflect.Field

import java.util.ArrayList

import java.util.HashMap

import java.util.Iterator

import java.util.List

import org.apache.commons.collections.functors.ForClosure

import org.apache.poi.hssf.usermodel.HSSFCell

import org.apache.poi.hssf.usermodel.HSSFWorkbook

import org.apache.poi.ss.usermodel.Cell

import org.apache.poi.ss.usermodel.Row

import org.apache.poi.ss.usermodel.Sheet

import org.apache.poi.ss.usermodel.Workbook

import org.apache.poi.xssf.usermodel.XSSFWorkbook

import org.slf4j.Logger

import org.slf4j.LoggerFactory

import com.alibaba.fastjson.JSONArray

import com.alibaba.fastjson.JSONObject

import com.erayt.ctms.system.standard.xnet.CtmsXNetContext

import com.erayt.xnet4j.XNetCommand

import com.erayt.xnet4j.XNetData

public class ReadForwardExcelServiceImpl implements ReadForwardExcelService {

private SearchPosDao searchForwardDao

public SearchPosDao getSearchForwardDao() {

return searchForwardDao

}

public void setSearchForwardDao(SearchPosDao searchForwardDao) {

this.searchForwardDao = searchForwardDao

}

protected Logger logger = LoggerFactory

.getLogger(this.getClass().getName())

private Workbook wb

//private JSONObject jsonObject = new JSONObject()

private List<Object>list_key = new ArrayList<Object>()

private List<Object>list_body = new ArrayList<Object>()

private List<HashMap<String,Object>>list_map = new ArrayList<HashMap<String,Object>>()

private CtmsXNetContext ctmsXNetContext

private String filename3

public String getFilename3() {

return filename3

}

public void setFilename3(String filename3) {

this.filename3 = filename3

}

public String getFilename4() {

return filename4

}

public void setFilename4(String filename4) {

this.filename4 = filename4

}

private String filename4

public void setCtmsXNetContext(CtmsXNetContext ctmsXNetContext) {

this.ctmsXNetContext = ctmsXNetContext

}

public void init() {

readXml(filename3)

}

public void readXml(String fileName) {

try {

InputStream input = new FileInputStream(fileName)// 建立输入流

// 根据文件格式2003来初始化

wb = new HSSFWorkbook(input)

Sheet sheet = wb.getSheetAt(0)// 获得第一个表单

Iterator<Row>rows = sheet.rowIterator()// 获得第一个表单的迭代器

for(int i=0i<sheet.getPhysicalNumberOfRows()i++) {

Row row = rows.next()// 获得行数据

//System.out.println(sheet.getPhysicalNumberOfRows()+"+++++")

Iterator<Cell>cells = row.cellIterator()// 获得第i行的迭代器

HashMap<String, String>HashBegain=new HashMap<String, String>()

for(int j=0j<row.getPhysicalNumberOfCells()j++){

//System.out.println(row.getPhysicalNumberOfCells()+"+++++"+j)

Cell cell = cells.next()

if(i==0||i==7){

list_key.add(cell.getStringCellValue())

}else {

if(cell.getCellType()== cell.CELL_TYPE_NUMERIC){

HashBegain.put((String) list_key.get(j), String.valueOf(cell.getNumericCellValue()))

}

else{HashBegain.put((String) list_key.get(j), cell.getStringCellValue())}

}

}

if(i!=0&&i!=7){

list_body.add(HashBegain)

}

if(i==6){

list_key = new ArrayList<Object>()

}

}

installJson()

readExcelEqual()

} catch (IOException ex) {

ex.printStackTrace()

}

}

// 组装即期交易json报文,并发送

public void installJson() {

String headStr = "{Head:" + "{\"chanelNo\":1508170004977,"

+ "\"dataCount\":\"1\"," + "\"eventType\":\"1\","

+ "\"msgNo\":\"1508170004977-20150917171754\","

+ "\"msgType\":\"1\"," + "\"sendingTime\":\"20150917171754\","

+ "\"source\":\"TR1\"," + "\"subSource\":\"TR1\","

+ "\"version\":\"1.0\"}," + "Body:"

+ JSONArray.toJSONString(list_body).replaceAll("\"", "'") + "}"

System.out.println(headStr)

try {

Thread.sleep(5000)

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace()

}

sendXNetTradeData(headStr, "RK1IN.Trade", "UTF-8")

}

// 发送到xnetd

public void sendXNetTradeData(String sendJson, String title, String charset) {

XNetData xnetData = new XNetData()

xnetData.setData(sendJson, charset)

xnetData.setTitle(title)

xnetData.setCommand(XNetCommand.TITLE)

if (ctmsXNetContext.getCtmsXnetSocket().isLoginOK()) {

ctmsXNetContext.getCtmsXnetSocket().sendData(xnetData)

} else {

logger.warn("CtmsXnet no login. sendXNetData[" + title

+ "] return.")

}

}

// 打印json属性

public static void printClassInfo(Object object) {

Field[] fields = object.getClass().getDeclaredFields()

for (int i = 0i <fields.lengthi++) {

try {

boolean accessFlag = fields[i].isAccessible()

fields[i].setAccessible(true)

String varName = fields[i].getName()

Object varValue = fields[i].get(object)

System.out.println(varName + "=" + varValue)

fields[i].setAccessible(accessFlag)

} catch (Exception e) {

e.printStackTrace()

}

}

}

//获取excel结果数据,并进行比较

public void readExcelEqual() {

//读取excel头寸数据

HashMap<String, String>HashEnd=new HashMap<String, String>()

Workbook wb1

List<Object>list_keyBody = new ArrayList<Object>()

List<HashMap>list_keyBody2 = new ArrayList<HashMap>()

//List<HashMap<String,Object>>list_keyBody2 = new ArrayList<HashMap<String,Object>>()

boolean END=false

try {

InputStream input = new FileInputStream(filename4)// 建立输入流

wb1 = new HSSFWorkbook(input)

Sheet sheet = wb1.getSheetAt(0)// 获得第一个表单

Iterator<Row>rows = sheet.rowIterator()// 获得第一个表单的迭代器

int i = 0

while (rows.hasNext()) {

HashEnd=new HashMap<String, String>()

Row row = rows.next()// 获得行数据

for (int j = 0j <row.getLastCellNum()j++) {

//System.out.println(row.getLastCellNum()+"XXXXX")

Cell cell = row.getCell(j)

if(i==0){

list_keyBody.add(cell.getStringCellValue())

}else{

if(cell.getCellType()==cell.CELL_TYPE_NUMERIC){

HashEnd.put((String) list_keyBody.get(j), String.valueOf(cell.getNumericCellValue()))

}

else

{

HashEnd.put((String) list_keyBody.get(j), cell.getStringCellValue())

}

}

}

if(i!=0){

list_keyBody2.add(HashEnd)}

i++

}

} catch (IOException ex) {

ex.printStackTrace()

}

//从数据库获取头寸数据

try {

Thread.sleep(10000)

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace()

}

list_map=searchForwardDao.findForwardPos()

//输出计算正确与否

for(int k=0k<list_map.size()k++){

if(END=list_map.get(k).get("AMOUNT_1").toString().equals((String) list_keyBody2.get(k).get("AMOUNT_1")))

{

logger.debug("远期第"+k+"组 货币1金额计算正确"+" Amount1="+list_map.get(k).get("AMOUNT_1").toString())

}else {

logger.debug("远期第"+k+"组 货币1金额计算不正确"+"left="+list_map.get(k).get("AMOUNT_1").toString()+" right="+list_keyBody2.get(k).get("AMOUNT_1"))

}

if(END=list_map.get(k).get("AMOUNT_2").toString().equals((String) list_keyBody2.get(k).get("AMOUNT_2")))

{

logger.debug("远期第"+k+"组 货币2金额计算正确"+" Amount2="+list_map.get(k).get("AMOUNT_2").toString())

}else {

logger.debug("远期第"+k+"组 货币2金额计算不正确"+"left="+list_map.get(k).get("AMOUNT_2").toString()+" right="+list_keyBody2.get(k).get("AMOUNT_2"))

}

if(END=list_map.get(k).get("START_PL").toString().equals((String) list_keyBody2.get(k).get("START_PL")))

{

logger.debug("远期第"+k+"组 累计损益计算正确"+" START_PL="+list_map.get(k).get("START_PL").toString())

}else {

logger.debug("远期第"+k+"组 累计损益计算不正确"+"left="+list_map.get(k).get("START_PL").toString()+" right="+list_keyBody2.get(k).get("START_PL"))

}

if(END=list_map.get(k).get("DAILY_PL").toString().equals((String) list_keyBody2.get(k).get("DAILY_PL")))

{

logger.debug("远期第"+k+"组 日止损计算正确"+" DAILY_PL="+list_map.get(k).get("DAILY_PL").toString())

}else {

logger.debug("远期第"+k+"组 日止损计算不正确"+"left="+list_map.get(k).get("DAILY_PL").toString()+" right="+list_keyBody2.get(k).get("DAILY_PL"))

}

}

}

}

int sum=1错了。

整型的最大值为2的32次。而1*2*3...*100=?远远超过这个值了,所以sum肯定溢出了。

可以是用大整数BigInteger 数值类型来存储结果


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11181950.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-14
下一篇 2023-05-14

发表评论

登录后才能评论

评论列表(0条)

保存